Comments (4)
Can you provide an example of the unit test?
from c-sharp-promise.
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.
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.
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)
- Feature Request: Promise retry HOT 2
- How to exit from then when check some conditions HOT 6
- Not Usable inside Tasks? HOT 3
- NuGet Package outdated since last improvements
- Cancelling a promise. HOT 1
- Default and custom error handler HOT 1
- GC caused by Closure HOT 3
- OpenUPM support. Unity Package Manager HOT 3
- A unsolved ambiguous compile error
- Compile Errors in Unity 2020.2.6f1 HOT 1
- For a complete noob... how and what do I import to my Unity project to start using this? HOT 1
- Different results for javascript.Promise and C-Sharp-Promise HOT 2
- Support .NET Standard and 3.5 into the same Unity project HOT 1
- Some general questions about handling promises HOT 3
- Promises are not Serializable HOT 1
- Need implicit casts between Promise<object> and Promise and interfaces HOT 3
- Catch from a Promise<object> returns IPromise instead of IPromise<object> HOT 2
- Promise Queue? HOT 2
- How can I get response data at the first stage HOT 7
- Create an Unity Package of this library HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-sharp-promise.