c# - 模拟对象仍在调用服务

标签 c# wcf unit-testing asp.net-mvc-4 justmock

所以我正在为我们的 MVC4 应用程序编写测试,并且我正在专门测试 Controller 操作。正如我在标题中提到的,测试仍然命中服务 (WCF) 而不是返回测试数据。我有这个 Controller :

public class FormController : Controller
{
    public SurveyServiceClient Service { get; set; }
    public SurveyDao Dao { get; set; }

    public FormController(SurveyServiceClient service = null, SurveyDao dao = null)
    {
        this.Service = service ?? new SurveyServiceClient();
        this.Dao = dao ?? new SurveyDao(Service);
    }

    //
    // GET: /Form/

    public ActionResult Index()
    {
        var formsList = new List<FormDataTransformContainer>();
        Dao.GetForms().ForEach(form => formsList.Add(form.ToContainer()));

        var model = new IndexViewModel(){forms = formsList};
        return View("Index", model);
    }

它使用这个 DAO 对象:

public class SurveyDao
{
    private readonly SurveyServiceClient _service;
    private readonly string _authKey;

    public SurveyDao(SurveyServiceClient serviceClient)
    {
        _service = serviceClient;
    }

    ....

    public FormContract[] GetForms()
    {
        var forms = _service.RetrieveAllForms();
        return forms;
    }

这是我使用 JustMock 进行的测试,GetForms() 上的模拟在辅助类中返回了一些测试数据:

[TestClass]
public class FormControllerTest
{
    private SurveyDao mockDao;
    private SurveyServiceClient mockClient;

    public FormControllerTest()
    {
        mockClient = Mock.Create<SurveyServiceClient>();
        mockDao = Mock.Create<SurveyDao>(mockClient);
    }

    [TestMethod]
    public void TestIndexAction() 
    {
        //Arrange
        var controller = new FormController(mockClient, mockDao);
        Mock.Arrange(() => mockDao.GetForms()).Returns(TestHelpers.FormContractArrayHelper);

        //Act
        var result = controller.Index() as ViewResult;

        //Assert
        Assert.IsInstanceOfType(result.Model, typeof(IndexViewModel));
    }
}

我的问题是,当我运行测试时,服务仍在被调用。我已经使用 Fiddler 验证了这一点,还调试了测试并检查了用我们服务的测试数据填充的“结果”的值。

编辑:

我已将测试构造函数更改为 [TestInitialize] 函数,因此测试现在看起来像这样:

[TestClass]
public class FormControllerTest
{
    private SurveyDao mockDao;
    private SurveyServiceClient mockClient;

    [TestInitialize]
    public void Initialize()
    {
        mockClient = Mock.Create<SurveyServiceClient>();
        mockDao = Mock.Create<SurveyDao>(Behavior.Strict);
    }

    [TestMethod]
    public void TestIndexAction() 
    {
        //Arrange
        var controller = new FormController(mockClient, mockDao);
        Mock.Arrange(() => mockDao.GetForms()).Returns(TestHelpers.FormContractArrayHelper);

        //Act
        var result = controller.Index() as ViewResult;

        //Assert
        Assert.IsInstanceOfType(result.Model, typeof(IndexViewModel));
    }
}

最佳答案

请确认您使用的是正确的 JustMock 程序集。有几个不同的(VisualBasic、Silverlight、JustMock)。 JustMock 是您应该包含在项目中的那个。

未能包含正确的将导致您描述的行为(方法未被正确 stub )。

关于c# - 模拟对象仍在调用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364487/

相关文章:

c# - 检查字符串是否出现在 JValues 列表中

c# - WCF 测试客户端无法添加服务,无法获取元数据

node.js - 测试 sails Controller

javascript - Jasmine JS覆盖没有仪器不工作

php - 为什么 error_log 仅使用 phpunit 的 --process-isolation 生成 RuntimeException

c# - Resharper 提议从 WPF 中的 Window 类中删除冗余继承

c# - 动态设置 WCF baseAddressPrefixFilters

c# - 通过 SqlDataAdapter 填充 DataSet 时超时

.net - WCF 服务中的依赖注入(inject)

c# - 在不同的 WCF 实例上共享数据状态