使用起订量来接受非灵长类动物的参数测试方法 [英] Using Moq to test methods that accept non-primative arguments

查看:91
本文介绍了使用起订量来接受非灵长类动物的参数测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个ASP.Net MVC控制器操作的测试。

I'm trying to write a test for an ASP.Net MVC controller action.

我想测试该动作调用上注入服务的特定方法,所以我嘲讽的服务和使用.Verify。

I'd like to test that the action invokes a particular method on an injected service, so I'm mocking the service and using .Verify.

因此​​,在简单的情况下,我在我的控制器中的下列行动:

So in the simple case, I have the following action in my controller:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(string title)
    {
        _cmsService.AddPage(title);

        return View("Edit");
    }

使用服务接口...

using the service interface...

public interface ICmsService
{
    void AddPage(string request);
}

和以下测试...

        //Arrange
        const string pageTitle = "Test Page";

        var cmsService = new Mock<ICmsService>();

        cmsService.Setup(service => service.AddPage(pageTitle));

        var controller = new PageController(cmsService.Object);

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

        //Assert
        cmsService.Verify(service => service.AddPage(pageTitle), Times.Once());

现在我想修改我的服务操作使用request和response对象...

Now I want to refactor my service operation to use request and response objects...

public interface ICmsService
{
    CmsServiceAddPageResponse AddPage(CmsServiceAddPageRequest request);
}

所以我相应地改变我的行动...

So I change my action accordingly...

    public ActionResult Create(string title)
    {
        var request = new CmsServiceAddPageRequest()
                          {
                              PageName = title
                          };

        var response = _cmsService.AddPage(request);

        return View("Edit");
    }

也是我的测试...

and also my test...

        //Arrange
        const string pageTitle = "Test Page";

        var cmsService = new Mock<ICmsService>();

        var request = new CmsServiceAddPageRequest() {PageName = pageTitle};

        cmsService.Setup(service => service.AddPage(request));

        var controller = new PageController(cmsService.Object);

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

        //Assert
        cmsService.Verify(service => service.AddPage(request), Times.Once());

但现在当我运行测试,我得到以下信息...

But now when I run the test, I get the following message...

TestCase 'Web.Test.PageControllerTest.CreateNewPagePost'
failed: Moq.MockException : 
Invocation was performed more than once on the mock: service => service.AddPage(value(Web.Test.PageControllerTest+<>c__DisplayClass1).request)
    at Moq.Mock.ThrowVerifyException(IProxyCall expected, Expression expression, Times times)
    at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
    at Moq.Mock.Verify[T,TResult](Mock mock, Expression`1 expression, Times times, String failMessage)
    at Moq.Mock`1.Verify[TResult](Expression`1 expression, Times times)
    PageControllerTest.cs(67,0): at Web.Test.PageControllerTest.CreateNewPagePost()

我应该怎么做来测试接受非原始类型的方法?

What should I be doing to test a method that accepts a non-primitive type?

感谢

沙地

推荐答案

我觉得第一个答案一个更好的选择将是实现一个自定义的匹配,而不是改变code,以配合您的测试框架。来源: HTTP://$c$c.google.com/p/moq /维基/快速入门

I think a better alternative to the first answer would be to implement a custom matcher rather than change code to match your testing framework. From:http://code.google.com/p/moq/wiki/QuickStart

// custom matchers
mock.Setup(foo => foo.Submit(IsLarge())).Throws<ArgumentException>();
...
public string IsLarge() 
{ 
  return Match<string>.Create(s => !String.IsNullOrEmpty(s) && s.Length > 100);
}

这篇关于使用起订量来接受非灵长类动物的参数测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆