使用Moq测试带有参数的特定方法调用 [英] Testing Specific Method Calls with Parameters using Moq

查看:81
本文介绍了使用Moq测试带有参数的特定方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Moq编写单元测试,以验证注册是否成功.我的测试如下:

I'm trying to write a Unit Test with Moq to verify that a Registration was successful. My Test is as follows:

    [TestMethod()]
    public void RegisterTest()
    {
        //Arrange
        var MockRepo = new Mock<IDataRepo>() ;             
        RegisterModel model = new RegisterModel
        {
            ConfirmPassword = "SamePassword",
            Email = "myemail@address.com",
            FirstName = "MyFirstName",
            LastName = "MyLastName",
            MiddleName = "MyMiddleName",
            Password = "SamePassword"
        };
        MockRepo.Setup(ctx => ctx.Add(model)).Verifiable("Nothing was added to the Database");

        //Act
        AccountController target = new AccountController(MockRepo.Object);

        //Assert
        ActionResult actual = target.Register(model);
        MockRepo.Verify(ctx => ctx.Add(It.IsAny<RegisterModel>()));
        Assert.IsInstanceOfType(actual, typeof(ViewResult));
    }

但是失败,并显示以下错误

But it fails with the following Error

 Expected invocation on the mock at least once, but was never performed: ctx => ctx.Add(It.IsAny()) 

但是,当我调试测试方法时,我注意到实际上调用了Add(T)方法. MOQ dll版本为v4.0

However, When I debugged the Test Method, I noticed that the Add(T) method was actually called. The MOQ dll version is v4.0

更新 帐户管理员:

public class AccountController : Controller
{

    private IDataRepo _repo;

    public AccountController(IDataRepo Repo)
    {
        _repo = Repo;
    }

    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            User user = _repo.Users.Where(u => u.Email == model.Email).FirstOrDefault();
            if (user == null)
            {
                _repo.Add(new User
                {
                    Email = model.Email,
                    Password = model.Password,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    MiddleName = model.MiddleName
                });

                return View("RegistrationSuccess");
            }
            else
            {
                ModelState.AddModelError("UserExists", "This Email already Exists");
            }
        }
        return View(model);
    }
}

推荐答案

您的问题是您的Mock需要一个RegisterModel实例

Your problem is that your Mock expects a RegisterModel instance

RegisterModel model = new RegisterModel
{
    ConfirmPassword = "SamePassword",
    Email = "myemail@address.com",
    FirstName = "MyFirstName",
    LastName = "MyLastName",
    MiddleName = "MyMiddleName",
    Password = "SamePassword"
};

MockRepo.Setup(ctx => ctx.Add(model))

,但是Add方法被User类的实例调用

but the Add method gets called with an instance of the User class

_repo.Add(new User
{
    Email = model.Email,
    Password = model.Password,
    FirstName = model.FirstName,
    LastName = model.LastName,
    MiddleName = model.MiddleName
});


因此,解决此问题的一种方法是将模拟程序设置为接受User实例.


So, one way to get around this is to setup the mock to accept a User instance.

RegisterModel model = new RegisterModel
{
    ConfirmPassword = "SamePassword",
    Email = "myemail@address.com",
    FirstName = "MyFirstName",
    LastName = "MyLastName",
    MiddleName = "MyMiddleName",
    Password = "SamePassword"
};
User expected = new User
{
    Email = model.Email,
    Password = model.Password,
    FirstName = model.FirstName,
    LastName = model.LastName,
    MiddleName = model.MiddleName
};
MockRepo.Setup(ctx => ctx.Add(expected))

这篇关于使用Moq测试带有参数的特定方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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