我在单元测试中使用Moq框架。 这是UpdateApplication测试方法:
[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
如何使用Moq编写更新和删除操作的单元测试?
答案 0 :(得分:3)
UpdateApplication单元测试方法的这种变体对我有用,但我不确定它是否正确:
[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());
var mockContext = new Mock<AppDbContext>();
mockContext.Setup(m => m.Applications).Returns(mockSet.Object);
// Act
var commandHandler = new UpdateApplicationCommandHandler(mockContext.Object);
var commandArg = new ApplicationCommandArg { Id = 1, Name = newAplpicationName };
commandHandler.Execute(new UpdateApplicationCommand(commandArg));
Assert.AreEqual(newAplpicationName, data.First(m => m.Id == 1).Name);
mockContext.Verify(m => m.SaveChanges(), Times.Once());
}
但我的DeleteApplicationCommandTest仍有问题。 当我运行测试时,我得到一个除了&#34;模拟上的预期调用正好3次,但是是2次:m =&gt; m.Applications&#34 ;. 这是测试方法:
[TestMethod]
public void DeleteApplication()
{
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());
var mockContext = new Mock<AppDbContext>();
mockContext.Setup(m => m.Applications).Returns(mockSet.Object);
// Act
var commandHandler = new DeleteApplicationCommandHandler(mockContext.Object);
var commandArg = new ApplicationCommandArg { Id = 1 };
commandHandler.Execute(new DeleteApplicationCommand(commandArg));
// Verify
mockSet.Verify(m => m.Remove(It.IsAny<Application>()), Times.Once());
mockContext.Verify(m => m.SaveChanges(), Times.Once());
mockContext.VerifyGet(m => m.Applications, Times.Exactly(3));
}
这是我的DeleteApplicationCommandHandler的执行方法:
public override void Execute(DeleteApplicationCommand command)
{
Debug.WriteLine("DeleteApplicationCommand executed");
var application = this.DbContext.Applications.FirstOrDefault(m => m.Id == command.CommandArg.Id);
if (application == null)
{
throw new Exception(string.Format("Application with id {0} was not found", command.CommandArg.Id));
}
this.DbContext.Applications.Remove(application);
this.DbContext.SaveChanges();
}
为什么DeleteApplication测试方法失败?
答案 1 :(得分:1)
问题是AddOrUpdate
是一种扩展方法。 Moq cannot mock extension methods,因此您需要找到另一种方法来实现测试覆盖率。