NSubstitute返回空字符串 [英] NSubstitute returning empty string

查看:69
本文介绍了NSubstitute返回空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试以下方法:

public HomeController(IUserIpAddressHelper userIpAddressHelper)
{
    _userIpAddressHelper = userIpAddressHelper;
}

[HttpGet]
public ActionResult Index()
{
    var userIpAddress = _userIpAddressHelper.GetUserIpAddress(System.Web.HttpContext.Current);
    if (_userIpAddressHelper.IsIpAddressOddOrEven(userIpAddress))
    {
        return RedirectToAction(HomePage);
    }
    
    return RedirectToAction(HomePageAlternative);
}

我正在测试如下:

public void Test()
{
    var userIpAddressHelper = Substitute.For<IUserIpAddressHelper>();
    userIpAddressHelper.GetUserIpAddress(Arg.Any<HttpContext>()).Returns("0.0.0.2");
  
    var controller = new HomeController(userIpAddressHelper);

    var result = controller.Index();

    Assert.IsInstanceOf<RedirectToRouteResult>(result);

    var redirectToRouteResult = result as RedirectToRouteResult;
    Assert.AreEqual(HomeController.HomePage, redirectToRouteResult.RouteValues["action"]);
}

但是,由于 userIpAddress 的值为""为空字符串而不是 0.0.0.2 ,因此测试失败我已经设定好了谁能指出我在哪里出问题了?

However the test is failing due to the value of userIpAddress being "" an empty string, instead of 0.0.0.2 as I've set it. Can anyone please point out where I've gone wrong here?

推荐答案

userIpAddress 肯定是"" ?看起来您原始测试中的 Returns 已正确指定,但是如果 IUserIpAddressHelper 是接口,则其替代品将不会存有 IsIpAddressOddOrEven ,因此即使将 GetUserIpAddress 存根以返回"0.0.0.2" ,它也会始终返回 false .

Is userIpAddress definitely ""? It looks like the Returns in your original test is specified well, but if IUserIpAddressHelper is an interface then the substitute for it will not have a result stubbed for IsIpAddressOddOrEven, so it will always return false even if GetUserIpAddress is stubbed to return "0.0.0.2".

要使测试反映生产代码如何通过数据,您可以将两个成员都存根:

To get the test to mirror how the production code passes through the data, you can stub out both members:

var userIpAddressHelper = Substitute.For<IUserIpAddressHelper>();
userIpAddressHelper.GetUserIpAddress(Arg.Any<HttpContext>()).Returns("0.0.0.2");
userIpAddressHelper.IsIpAddressOddOrEven("0.0.0.2").Returns(true);

这将测试生产代码是否正确地将 GetUserIpAddress 的结果传递给 IsIpAddressOddOrEven .

This will test that the production code correctly passes through the result of GetUserIpAddress to IsIpAddressOddOrEven.

注意:我们也可以将其存根以与"ip-address-result" 一起使用,并且仍然可以使用.我们不需要返回有效的奇/偶结果,因为我们没有使用 IUserIpAddressHelper 的真实实现,而只是替代测试.如果您发现有必要在许多测试中替换 IUserIpAddressHelper ,并且希望它像真实的实现一样工作(即,它实际上将返回地址是奇数还是偶数),则可能更容易实现编写一个 TestUserIpAddressHelper .

Note: we could also stub these to work with "ip-address-result" and it would still work. We don't need a valid odd/even result returned, as we are not using a real implementation of IUserIpAddressHelper, just a substitute for testing. If you find it necessary to substitute for IUserIpAddressHelper in lots of tests and you want it to act like a real implementation (i.e. it will actually return whether an address is odd or even), it might be easier to write a TestUserIpAddressHelper.

另一种避免在 GetUserIpAddress IsIpAddressOddOrEven 的结果之间具有依赖性的方法是将接口更改为具有 bool IsIpAddressOddOrEven(HttpContext context)的接口.code>结合了这两种操作的方法.这样一来,您只需要存根即可进行测试.

Another way to avoid having the dependency between the results of GetUserIpAddress and IsIpAddressOddOrEven is to change the interface to have a bool IsIpAddressOddOrEven(HttpContext context) method that combines both operations. That way you would only need to stub one for the test.

这篇关于NSubstitute返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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