0

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

    [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));
    }

但它失败并出现以下错误

对模拟的预期调用至少一次,但从未执行过:ctx => ctx.Add(It.IsAny())

但是,当我调试测试方法时,我注意到实际上调用了 Add(T) 方法。MOQ dll 版本为 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);
    }
}
4

2 回答 2

2

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

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类的实例调用

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

因此,解决此问题的一种方法是设置模拟以接受User实例。

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))
于 2012-08-03T11:32:17.147 回答
0

我发现了一种更简单的方法。而不是生成您自己的用户对象,您可以调用It.IsAny<User>()并且测试会运行得很好。所以我的单元测试现在变成了..

        //Arrange
        var MockRepo = new Mock<IDataRepo>() ;             
        var MockMembership = new Mock<IMembership>();
        RegisterModel model = new RegisterModel
        {
            ConfirmPassword = "SamePassword",
            Email = "myemail@address.com",
            FirstName = "MyFirstName",
            LastName = "MyLastName",
            MiddleName = "MyMiddleName",
            Password = "SamePassword"
        };

        MockRepo.Setup(ctx => ctx.Add(It.IsAny<User>())).Verifiable("Nothing was added to the Database");
        //Act
        AccountController target = new AccountController(MockRepo.Object, MockMembership.Object);

        //Assert
        ActionResult actual = target.Register(model);
        MockRepo.Verify(ctx => ctx.Add(It.IsAny<User>()));
        Assert.IsInstanceOfType(actual, typeof(ViewResult));
于 2012-08-06T08:46:13.487 回答