Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify no other calls #1991

Open
jerone opened this issue Dec 8, 2022 · 1 comment
Open

Verify no other calls #1991

jerone opened this issue Dec 8, 2022 · 1 comment

Comments

@jerone
Copy link

jerone commented Dec 8, 2022

Feature Proposal

I want to verify that no other calls have been made after verifying the known call.

Context

Some mocked spies can have many methods. Instead of checking every method to not have been called, I'm looking for a way to do this automatically.

I know there is toHaveSpyInteractions, but that only checks if any interactions have been made.

Example

I want to write something like this:

describe("Multiple spies", function () {
  let person;
  beforeEach(function () {
    person = jasmine.createSpyObj('person', ['eat', 'drink', 'sleep', 'walk']);
    person.eat();
  });
  it("creates spies for each requested function", function () {
    expect(person.eat).toHaveBeenCalled();
    expect(person).verifyNoOtherCalls(); // <-- feature request.
  });
});

Instead of having to write something like this:

describe("Multiple spies", function () {
  let person;
  beforeEach(function () {
    person = jasmine.createSpyObj('person', ['eat', 'drink', 'sleep', 'walk']);
    person.eat();
  }); 
  it("creates spies for each requested function", function () {
    expect(person.eat).toHaveBeenCalled();
    expect(person.drink).not.toHaveBeenCalled();
    expect(person.sleep).not.toHaveBeenCalled();
    expect(person.walk).not.toHaveBeenCalled();
  });
});

Other Information

In C# there is something similar for Moq, called VerifyNoOtherCalls.

Looking through the source-code, it looks like this would only be possible if the toHaveBeenCalled method (and similar) sets some kind of isVerified state to true in the CallTracker most likely. Exactly like Moq does.

@sgravrock
Copy link
Member

That seems like it would be a nice addition. I occasionally use the equivalent Mockito facility. I'd rather call the matcher toHaveNoOtherSpyInteractions or toHaveNoMoreSpyInteractions for consistency and to make the expectation read better, but other than that it makes sense to me. Want to submit a PR?

Ulys added a commit to Ulys/jasmine that referenced this issue Feb 22, 2023
Ulys added a commit to Ulys/jasmine that referenced this issue Feb 23, 2023
Ulys added a commit to Ulys/jasmine that referenced this issue Feb 23, 2023
Ulys added a commit to Ulys/jasmine that referenced this issue Feb 24, 2023
Ulys added a commit to Ulys/jasmine that referenced this issue Mar 5, 2023
Ulys added a commit to Ulys/jasmine that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants