Mobx.dart: Testing observables with reactions

Created on 9 Mar 2020  路  9Comments  路  Source: mobxjs/mobx.dart

Hi,

I'm face a problem that I don't know how to solve it. I have two observables and when one received a value, the other receive a value from a service. That means it all are asynchronous and when I attempt to test it, It fails.

So. I need a way to test observables with reactions.

Here a sample code to explain better this problem.

https://github.com/TercyoStorck/mobx_reaction_test

Most helpful comment

Great points Michael. You should definitely write a testing guide for the site!

All 9 comments

If I test the reaction without a Future call, in other words, if a directly change the value of observable inside de reaction, I can test the reaction.

And the test pass too if I put a delay of Duration.zero before the expect.

There's something that we should do to test reactions that call asynchronous a test that I'm missing?

Would asyncWhen help somehow?

Would asyncWhen help somehow?

In one case when there's the need of when I think so, but in this case, the reaction needs be triggered in every change of the observable.

Also I think that asyncWhen auto-dispose like when after effect runs.

@pavanpodila Can you help us? Thank you! =)

@TercyoStorck take a look at the PR I submitted back to your repo here where asyncWhen is used in the code for testing not within the actual logic of your app

Another thing to add is to not block your tests from stalling indefinitely (?), when awaiting a reaction that fails to trigger (e.g. when you have a bug in your code) is to set a timeout on the Future returned by asyncWhen

Great points Michael. You should definitely write a testing guide for the site!

Good idea but not sure when and if I can get around to that the at the moment. Perhaps let's create an issue for that to open that up for others to contribute as well. I'm going to close this one as @TercyoStorck's should be resolved now

@TercyoStorck take a look at the PR I submitted back to your repo here where asyncWhen is used in the code for testing not within the actual logic of your app

It worked very well. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

campanagerald picture campanagerald  路  3Comments

vadimtsushko picture vadimtsushko  路  5Comments

jascodes picture jascodes  路  5Comments

Tywholland picture Tywholland  路  3Comments

viktorszekeress picture viktorszekeress  路  4Comments