Giter Club home page Giter Club logo

Comments (4)

ashleydavis avatar ashleydavis commented on July 17, 2024

Can you provide an example of the unit test?

from c-sharp-promise.

mikecann avatar mikecann commented on July 17, 2024
public class ExamplePromiseTests
{   
    [SetUp]
    public void Setup()
    {
        Promise.UnhandledException += OnUnhandledException;
    }

    [TearDown]
    public void TearDown()
    {
        Promise.UnhandledException -= OnUnhandledException;
    }

    private void OnUnhandledException(object sender, ExceptionEventArgs e)
    {
        throw e.Exception;
    }

    [Test]
    [ExpectedException(typeof(Exception))]
    public void ExceptionOnPromiseError()
    {
        DoThingAsync();
    }

    private IPromise DoThingAsync()
    {
        return new Promise((resovle, reject) =>
        {
            throw new Exception("whoops");
        });
    }
}

BTW how does one test async code in Unity without async / await?

from c-sharp-promise.

ashleydavis avatar ashleydavis commented on July 17, 2024

What you are trying to do in that test won't work.

Exceptions don't work in the regular way with promises. You have to handle Catch if you want to test that an exception happened.

I'm not sure how you'd do this in your testing framework... in Xunit.net I'd do this:

[Fact]
public void MyTest()
{
     Exception caughtException = null;

    DoThingAsync()
        .Catch(ex => caughtException = ex);

    Assert.NotNull(caughtExpection);        
}

This particular test isn't actually asynchronous because you directly throw the exception.

You can test async code by mocking out dependencies and having them return a promise that you resolve synchronously in the test. This is the way we usually test async code at Real Serious Games.

Are you using a mocking library? We use Moq.

You can do true async testing with Xunit.net and this should work for you in the Visual Studio and Xunit tests runners, but probably won't work if you want to run your unit tests under Unity (http://bradwilson.typepad.com/blog/2012/01/xunit19.html).

from c-sharp-promise.

mikecann avatar mikecann commented on July 17, 2024

Hi Ashley,

Thanks for getting back.

Okay ye I thought as much, thats pretty much how I have been doing it (with the Catch) I was hoping to make it neater by using the expected exception.

As for the Async tests ye thats how I have been doing it too with NSubstitute, I was just wondering if there was a different way that people do it in Unity, I guess not!

Thanks again :)
Mike

from c-sharp-promise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.