我在我的單元測試中使用了Moq框架。 這是UpdateApplication測試方法:更新,使用Moq刪除單元測試的方法
[TestMethod]
public void UpdateApplication()
{
const string newAplpicationName = "NewApplication1";
var data =
new[]
{
new Application { Id = 1, Name = "Application1" }, new Application { Id = 2, Name = "Application2" },
new Application { Id = 3, Name = "Application3" }, new Application { Id = 4, Name = "Application4" }
}
.AsQueryable();
var mockSet = new Mock<DbSet<Application>>();
mockSet.As<IQueryable<Application>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Application>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Application>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Application>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
mockSet.Setup(m => m.AddOrUpdate(It.IsAny<Application[]>())).Callback(
(Application[] apps) =>
{
apps.FirstOrDefault(m => m.Id == 1).Name = newAplpicationName;
}).Verifiable(); // <-- Exception
var mockContext = new Mock<AppDbContext>();
mockContext.Setup(c => c.Applications).Returns(mockSet.Object);
// Act
var commandHandler = new UpdateApplicationCommandHandler(mockContext.Object);
var commandArg = new ApplicationCommandArg { Id = 1, Name = newAplpicationName };
commandHandler.Execute(new UpdateApplicationCommand(commandArg));
// Verify
mockContext.Verify(m => m.SaveChanges(), Times.Once());
}
我得到了一個異常時,運行測試:
An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code Additional information: Expression references a method that does not belong to the mocked object: m => m.AddOrUpdate(It.IsAny()) at Moq.Mock.ThrowIfNotMember(Expression setup, MethodInfo method) at Moq.Mock.c__DisplayClass19`1.b__18() at Moq.PexProtector.Invoke[T](Func`1 function) at Moq.Mock.Setup[T](Mock`1 mock, Expression`1 expression, Condition condition) at Moq.Mock`1.Setup(Expression`1 expression) at UpdateApplication() in UpdateApplicationCommandTests.cs:line 39
我應該如何寫更新單元測試和刪除使用起訂量的行動?
http://stackoverflow.com/questions/2295960/mocking-extension-methods-with-moq – dee 2014-12-01 16:36:20
謝謝你,這是非常有用的鏈接。 – Vitone 2014-12-02 08:52:57