The Purpose of Mocking

后端 未结 8 461
独厮守ぢ
独厮守ぢ 2020-12-29 15:33

What is the purpose of mocking?

I have been following some ASP.NET MVC tutorials that use NUnit for testing and Moq for mocking. I am a little unclear about the mock

8条回答
  •  抹茶落季
    2020-12-29 16:09

    One other answer :

    • Stub = fake objects to be able to run your test without the entire real context

    • Mock = fake object to record the interaction of your component and verify theses interactions

    You can have several stubs in one test but only one mock because if you have more than one mock you certainly test more than one feature (and it defeat the purpose of the test-one-thing principle).

    To go beyond the basics, Mocks are more behavioral verification than the traditionnal state verification of the test where you check the state of your component after acting on it (Arrange,Act, Assert with Mocks it is more Arrange, Act, Verify) :

    State verification Assert.AreEqual(valueExpected,mycomponent.Property); Behavioral verification : myMock.WasCalled(MyMethod);

提交回复
热议问题