2

我知道一些 ASP MVC,试图拥抱 TDD。以下示例安装了 xUnit 和 TestDriven(包括 Moq)。

问题是我试图模拟图像上传视图模型,以便我可以断言它正在被上传。

起订量给了我问题:

非虚拟(在 VB 中可覆盖)成员上的设置无效

在试图

var imageMock = new Mock<ImageViewModel>();
imageMock.Setup(x => x.IsUrl).Returns(true);`

我不知道如何继续下去——为视图模型创建接口是胡说八道,我是为站点编程,而不是为测试环境编程。

我应该替换 Mock 环境或定义接口还是...?

请提供一些经验丰富且内容丰富的建议,并请提供或至少喜欢您建议我做的事情的好样品。

谢谢!

4

1 回答 1

1

First of all, is this a auto property?

public bool IsUrl {get; set;}

If so, just set the value yourself in the set up of your test. If it's not an auto property, does it make more sense to move it into a method, instead of property. And at that time, you could make the method virtual (which is what the error message is actually saying.)

When mocking, you can't mock things that are either not interfaces or are not virtual (I believe there are some paid mocking libraries that let you, but FakeItEasy, Moq and others require that it be virtual.)

To do this, you would simply need to make the property look like this:

public virtual bool IsUrl {get; set;}

Secondly, what are you testing on your view model? Testing getters and setters is largely a waste of time because they will most likely be tested in other places of your code. Plus, tests on getters and setters are testing the compiler, not your code. If getters and setters don't work in .NET you've got a whole host of problems. It would be better to test the creation of your view model and then make sure that it has the right values after creation.

于 2013-03-26T21:50:50.717 回答