How to call toHaveBeenCalledTimes in jest on condition - unit-testing

I am trying to call a function and checking it with toHaveBeenCalledTimes whether it has been called or not in jest, that function runs on a condition, how I can check it on condition?

Related

How can I use mockito to verify that a function has never been called, with any argument?

I want to verify that a function has never been called using mockito. I am aware of the verifyNever function, but that does not seem to work in my case, or I'm not using it correctly.
My test currently looks like this:
test('when the string is less than 3 characters api is not called', () async {
var databaseService = getAndRegisterDatabaseService();
var model = ViewModel();
model.searchPatient('sk');
// Wait for futures to finish.
await Future.delayed(Duration(seconds: 0));
verifyNever(databaseService.searchPatient('sk'));
});
but I get an error when running the test:
No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
To me it seems like verifyNever won't work if the function has never been called, just that it has never been called with a specific argument. Is that correct? In that case, is there another way to test what I want?
The verifyNever examples from package:mockito's README.md cover your case:
// Or never called
verifyNever(cat.eatFood(any));
So, in your case, assuming that databaseService is a Mock object, you should be able to use verifyNever(databaseService.searchPatient(any)); to verify that the .searchPatient method is never called, regardless of the arguments.
After trying to write a minimal reproducible example it turns out that the problem was that I read the output incorrectly.
In the following output I read the top row as the title for the failed test, when in fact it is the bottom row that is the name of the failed error than corresponds to the above output.
✓ ViewmodelTest - searchPatient - when the string is less than 3 characters api is not called
No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
package:test_api fail
_VerifyCall._checkWith
package:mockito/src/mock.dart:672
_makeVerify.<fn>
package:mockito/src/mock.dart:954
main.<fn>.<fn>.<fn>
test/viewmodel_tests/viewmodel_test.dart:144
✖ ViewmodelTest - searchPatient - api is called with correct and cleaned query

Clearing mocks after each test jest

I am learning Jest and I see this clearAllMocks function being used, I then check the docs and the description is simply this:
Clears the mock.calls and mock.instances properties of all mocks.
Equivalent to calling .mockClear() on every mocked function.
Returns the jest object for chaining.
It basically says what you could already figure out by reading the function name. I still can't figure out when should I use this and why is this useful. Could you name an example when this would be good to use?
This can be set in Jest config file which is equivalent to calling jest.clearAllMocks() before each test.
https://jestjs.io/docs/configuration#clearmocks-boolean
// jest.config.js
{
// ...rest
"clearMocks": true
}
jest.clearAllMocks() is often used during tests set up/tear down.
afterEach(() => {
jest.clearAllMocks()
});
Doing so ensures that information is not stored between tests which could lead to false assertions. Let's say that you have a mock function mockFn and you call the function, you can assert that it's been called 1 time. If in another test you call mockFn again but you have not cleared the mock, it would have been called two times now instead of one.

Spying a method which is in the tested module

To make it clear I'll 'draw' you a situation.
Module A(the same javascript file, at my case its reducer in ReactJS/Redux app) contains methods: First, Second
Im testing method First (inside which Second is called - and I want to check whether it has been called or not). So in pseudo code it looks like this:
creating a spy with sinon.spy(module1, 'Second')
invoking First()
checking whether its been called or not _createdSpy.should.have.calledOnce
The problem Ive encountered is that if the method Second is placed at the same module (file) as the First, it doesnt work (library doesnt detect if spy has been called). If I move it to the another file/module, it works just fine.
its by design or Im doing something wrong?
Im using sinon + chai + enzyme stack.

MOQ Unit Test - Assert Vs Verify

Am trying to understand what Exactly Verify or VerifyAll Does ?
I was searching and i got the below info on using MOQ
Arrange
Mock
Set up expectations for dependencies
Set up expected results
Create class under test
Act
Invoke method under test
Assert
Assert actual results match expected results
Verify that expectations were met
So What exactly does Verify Does? I can test everything using Assert and in case if any failures the unit test will fail ?
What extra work does verify does ? Is it the replacement for Assert ?
Some more clarify will make me understand.
Thanks
Assert vs Mock.Verify
Assert is used to do checks on the class/object/subject under test (SUT).
Verify is used to check if the collaborators of the SUT were notified or contacted.
So if you are testing a car object, which has an engine as a collaborator/dependency.
You would use to Assert to see if car.IsHumming after you invoke car.PushStart()
You would use Verify to see if _mockEngine.Ignition() received a call.
Verify vs VerifyAll
Approach One:
Explicitly setup all operations you expect to be triggered on the mocked collaborator from the subsequent Act step
Act - do something that will cause the operations to be triggered
call _mock.VerifyAll() : to cause every expection that you setup in (1) to be verified
Approach Two
Act - do something that will cause the operations to be triggered
call _mock.Verify(m => m.Operation) : cause verification that Operation was in fact called. One Verify call per verification. It also allows you to check count of calls e.g. exactly Once, etc.
So if you have multiple operations on the Mock OR if you need the mocked method to return a value which will be processed, then Setup + Act + VerifyAll is the way to go
If you have a few notifications that you want to be checked, then Verify is easier.

Does ?assertException actually work with gen_fsm:startlink?

I have the following init method in a gen_fsm callback module (my_fsm):
init(Args) when length(Args) =:= 2 ->
% do work with Args here
{ok, my_state, InitialState}.
There is no other init-method in the callback module.
I want to write a unit test using eunit where I assert that calling gen_fsm with an argument-list that does not contain two elements fails:
start_link_without_args_exits_test() ->
?assertException(exit, _, gen_fsm:start_link(my_fsm, [], [])).
However, when the test is run, eunit skips the test with the message:
undefined
*unexpected termination of test process*
::{function_clause,[{my_fsm,init,[[]]},
{gen_fsm,init_it,6},
{proc_lib,init_p_do_apply,3}]}{proc_lib,init_p_do_apply,3}]}
Why is it that the test does not "catch" this error and reports it as a passed test?
The exception actually happens in the gen_fsm process. When the call to the init/1 function is made, gen_fsm catches the result. If it sees something like an error, it will send the error back to the parent (through proc_lib:init_ack/1-2) and then call exit(Error).
Because you use start_link and are not trapping exit, you will never receive the return value -- you'll simply crash instead. For your test case, you'll need to either use start or call process_flag(trap_exit,true) in order to obtain the return value rather than just crashing when the other process goes down.
Then you'll need to switch from
?assertException(exit, _, gen_fsm:start_link(my_fsm, [], [])).
to something like
?assertMatch({error,{function_clause,[{my_fsm,init,_} | _]}},
gen_fsm:start_link(my_fsm, [], []))
In order to have it working well.