我正在通過本教程http://blogs.telerik.com/justteam/posts/13-10-25/30-days-of-tdd-day-17-specifying-order-of-execution-in-mocks關於TDD。我正試圖將一個JustMock聲明改編成Moq。以下設置不匹配 - 將JustMock轉換爲Moq
enter code here [Test]
public void testname()
{
var customerId = Guid.NewGuid();
var customerToReturn = new Customer { Id = customerId};
//JustCode
Mock _customerService = Mock.Create<ICustomerService>();
Mock.Arrange(() => _customerService.GetCustomer(customerId)).Returns(customer).OccursOnce();
//Moq
Mock<ICustomerService> _customerService = new Mock <ICustomerService>();
_customerService.Setup(os => os.GetCustomer(customerId)).Returns(customerToReturn);
_customerService.VerifyAll();
}
當測試跑了,我得到這個異常:
Moq.MockVerificationException : The following setups were not matched:ICustomerService os => os.GetCustomer(a1a0d25c-e14a-4c68-ade9-bc3d7dd5c2bc)
當我改變.VerifyAll()來.Verify(),測試通過,但我不確定這是否正確。
問題:什麼是適應此代碼的正確方法? .VerifyAll()與.OccursOnce()不相似嗎?
下面的答案有幫助嗎? – Spock
是的,謝謝你的兩個例子。 – derguy