在測試方法中傳遞對象參數的最佳做法是什麼? 在這種情況下使用什麼(嘲笑,存根或創建對象的實例並填寫)如何在測試方法中使用模擬參數或實例參數?
例如:
[Test]
public void LoadContentsFor_ValidUser_ReturnsEmptyList()
{
var user = new User {Id = 1, FirstName = "Test"};
var contents = this.contentPresentationService.LoadContentsFor(user);
Assert.IsTrue(contents.Count == 0);
}
或者用嘲笑:
[Test]
public void LoadContentsFor_ValidUser_ReturnsEmptyList()
{
var user = new Mock<User>();
user.Setup(x => x.Id).Returns(1);
user.Setup(x => x.FirstName).Returns("Test");
var contents = this.contentPresentationService.LoadContentsFor(user.Object);
Assert.IsTrue(contents.Count == 0);
}