Could replace VS Assert standard testing?

I am trying to figure out how to use Moq, however I have some confusion as to why Moq should be used. My understanding is that the layout structure is used to create objects that are difficult to create under normal circumstances. However, the examples that I saw in Moq seem to not only create a Moq object, but also provide methods for testing objects - this seems to eliminate the need for the usual Assert methods, etc., which we use for most unit tests.

Can someone confirm if I correctly believe that Moq is replacing Assert, etc., or am I not completely losing the Moq point?

+4
source share
2 answers

A mocking structure such as Moq is not fully replaces the Assert testing platform. sometimes it happens, sometimes it is not.

Let mocks and stubs be distinguished first. Billets are used solely for isolation and for introducing any controlled behavior into the system under test (SUT).

Mocks is a superset of stubs, and they can check if something was called on a fake call. In Moq, calling Verify() makes a joke a mock regarding this method. Calling VerifyAll() makes all the methods that mocks configured.

The recommended approach is that you should have no more than one layout in your test. In this sense, it is similar to Assert - that you should not test multiple tests in a test.

Returning to the original question. If you perform state testing, you will use zero or more stubs, and one will state. If you conduct interaction testing, you will use zero or more butts and one layout. The following is an example where it might be appropriate to use both layouts and statements to test the same service.

 public interface IAccountRepository { decimal GetBalance(int accountId); void SetBalance(int accountId, decimal funds); } public class DepositTransaction { IAccountRepository m_repo; public DepositTransaction(IAccountRepository repo) { m_repo = repo; } public decimal DepositedFunds {get; private set;}; void Deposit(int accountId, decimal funds) { decimal balance = m_repo.GetBalance(accountId); balance += funds; m_repo.SetBalance(balance); DepositedFunds += funds; } } public class DepositTest { [TestMethod] void DepositShouldSetBalance() { var accountMock = new Mock<IAccountRepository>(); accountMock.Setup(a=>a.GetBalance(1)).Returns(100); //this is a stub with respect to GetBalance var transation = new DepositTransaction(accountMock.Object); transation.Deposit(1, 20); accountMock.Verify(a=>a.SetBalance(1, 120)); //this is a mock with respect to SetBalance } [TestMethod] void DepositShouldIncrementDepositedFunds() { var accountMock = new Mock<IAccountRepository>(); accountMock.Setup(a=>a.GetBalance(1)).Returns(100); //this is a stub with respect to GetBalance var transation = new DepositTransaction(accountMock.Object); transation.Deposit(1, 20); transation.Deposit(1, 30); Assert.AreEqual(50, transaction.DepositedFunds); } } 
+4
source

+1 on Q and the other A ...

(Igor’s repetition of a very complete answer) The Mocking library is simply using the tool in a test / specification - sometimes playing the role of a stub or layout.

Bullying-based recording / playback tracking can often replace the Assert role in your tests. This style was recently filmed at RhinoMocks (I believe that 4.0 was going to shunt it to the side), and for some time it was actively discouraged. I believe that this is what made you ask the question.

You must optimize your tests to:

  • Being readable by you, maintenance programmers, and almost everyone who will ever have to debug or extract information from your test code.
  • Confirm as few things as possible
  • Do not override other tests
  • Do not be fragile - tests should not break for random reasons when you change something only related to your test. In the context of moking and ridicule, this means that in most cases it deviates from severe bullying.
  • Do as little work as possible to prove your point.
  • Clear separation of Arrange / Context, Act, and Assert sections

The last point here is critical - you want the bit where you do the checks to be final. Sometimes this can mean that you can even make a Setup call in Moq in the Arrange / Context phase, and then verify that it was used in the Assert phase with code similar to duplication.

+2
source

All Articles