How do you unit test unique method calls in Jasmine/Jest?

Danchez

I have code in my application where a certain function is called n-times (lets call this function foo). The runtime expectation is that each invocation of foo is unique (called with a unique set of arguments). I had a recent bug manifest in my app due to foo being called with the same set of arguments more than once.

I want to write a test case where I can assert that foo was uniquely called once with a specific set of args but I'm at a loss at how to do so in Jasmine/Jest.

I know Jasmine has the toHaveBeenCalledOnceWith matcher but that asserts that foo was called "exactly once, and exactly with the particular arguments" which is not what I'm looking for in this scenario.

Ryan Schaefer

You can combine toHaveBeenCalledWith() and toHaveBeenCalledTimes() to get your desired behaivor, you just need to have as many toHaveBeenCalledWith()s as the number of calls you were expecting:

for ex:

describe("test", () => {
  it("should call not more than unique", () => {
     spyOn(callsFoo, 'foo');
     callsFoo.somethingThatCallsFoo();
     expect(callsFoo.foo).toHaveBeenCalledTimes(2);
     expect(callsFoo.foo).toHaveBeenCalledWith({...someArgs});
     expect(callsFoo.foo).toHaveBeenCalledWith({...otherUnique});
  });
})

This would fail if there were repeated calls that weren't unique.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you unit test Google Cloud NDB code?

How to write a unit test for a method which calls other services

How do I unit test this doFilter() method?

How do you unit test a Celery task?

How do you unit test a Celery task?

How do you unit test a JavaFX controller with JUnit

How do you unit test Java EE code?

How do you perform unit test on a particular method of a struct in Go

How do you Unit Test Python DataFrames

How do you override a module/dependency in a unit test with Dagger 2.0?

How do you skip a unit test in Django?

How do you unit test private methods?

How do you write a unit test for a function that is not exported?

How do you unit test office.js app code?

How to unit test a function in a component that calls method to parent component

How do I unit test a taglib that calls g.formatDate?

How do you unit test Django RawQuerySets

How do you moq a class for unit test

How do you write a test against a function that calls exit()?

How do I test the order of method calls in rspec?

How to unit test a public method that calls private method and that in turn calls a web service method

How do you use test-unit?

If a method calls another method, is it a unit test or integration test?

How do you test django-axes with a Django unit test?

Python Unit test how to test a method which calls another method

How to unit test a method which calls a void method?

How do I unit test an instantiated class that is also retuned by a method?

How do you deal with a fluctuating unit test case with Jasmine?

In Scala, how do I properly unit test a class method that calls other class methods?