Note that there are some explanatory texts on larger screens.

plurals
  1. POMoq test passes even when verifying both Times.Once() and Times.Never() on same method call
    primarykey
    data
    text
    <p>I'm using <code>Xunit</code> to test the <code>Create</code> method on my <code>CarController</code> and I'm using <code>Moq</code> to mock my <code>CarRepository</code>.</p> <p>I'm then using <code>mockCarRepository.Verify(m =&gt; m.Create(It.IsAny&lt;Car&gt;()), Times.Once());</code> to check that the <code>Create</code> method on my repository is being called. However the test passes irrespective of whether I call it or not.</p> <p>Here is a complete example where I verify <code>both that Create is called once AND that it is called never</code>. My test passes when I would have expected it to fail.</p> <pre><code>using System; using Moq; using Xunit; namespace Test { public class CarTest { [Fact()] public async void CreateTest() { var mockCarRepository = new Mock&lt;CarRepository&gt;(); var carController = new CarController(mockCarRepository.Object); carController.Create(new Car { Make = "Aston Martin", Model = "DB5" }); mockCarRepository.Verify(m =&gt; m.Create(It.IsAny&lt;Car&gt;()), Times.Once()); mockCarRepository.Verify(m =&gt; m.Create(It.IsAny&lt;Car&gt;()), Times.Never()); } } public class CarController { private readonly CarRepository _repo; public CarController(CarRepository repo) { _repo = repo; } public void Create(Car car) { _repo.Create(car); } } public class Car { public virtual String Make { get; set; } public virtual String Model { get; set; } } public class CarRepository { public virtual void Create(Car car) { // DO SOMETHING } } } </code></pre> <p>When I debug the test, although it still passes, I notice that the following exception is thrown:</p> <pre><code>A first chance exception of type 'Moq.MockException' occurred in Moq.dll Additional information: Expected invocation on the mock should never have been performed, but was 1 times: m =&gt; m.Create(It.IsAny&lt;Car&gt;()) No setups configured. Performed invocations: CarRepository.Create(Test.Car) </code></pre> <p>The exception is expected as I am calling <code>Create</code> once and verifying <code>Times.Never()</code> but I would like my test to fail. What do I need to do to achieve this?</p> <p><strong>Update</strong> It turns out that the problem was that I'd marked my test as <code>async</code> - removing that causes it to pass. However the actual code I'm writing will call an <code>async</code> method, so my question is now, how can I verify methods are called when using asynchronous methods?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload