MVC3 Moq 帐户控制器 c# nUnit

发布于 2024-12-05 14:00:35 字数 207 浏览 0 评论 0原文

我正在尝试学习最小起订量,但事实证明这有点困难。

如果我想在新的 MVC3 项目中使用 nUnit 和 Moq 为帐户控制器实现一些基本测试,我该怎么做?

我习惯了实体框架。但没有为其构建接口。

编辑: 我理解这一切的理论以及这样做的必要性,但是实现它让我感到困惑我

一直在使用实体代码生成器(dbContext)来生成可用于接口的代码

I am trying to learn Moq but it is proving somewhat difficult.

If I want to implement some basic tests using nUnit and Moq for the account controller in a new MVC3 project, how would I go about it?

Im used to the entity framework. but not building interfaces for it.

edit:
I understand the theory of it all and the need to do it, but implementing it is confusing me

I have been using Entity Code generator (dbContext) to generate code I can use for interfaces

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放手` 2024-12-12 14:00:35

好的,这是一个很好的测试:当您注册新用户时,您希望确保他会自动登录网站,这样他就不需要再次输入用户名和密码。

测试将是这样的:

public void AutomaticallySignedInAfterRegistering()
{
    var membershipService = new Mock<IMembershipService>();
    var formsService = new Mock<IFormsAuthenticationService>();
    RegisterModel newUser = new RegisterModel();
    newUser.UserName = "John"
    newUser.Email = "[email protected]"
    newUser.Password = "p@ss";
    newUser.ConfirmPassword = "p@ss";

    membershipService.Setup(x => x.CreateUser("John", "p@ss", "[email protected]")).Returns(MembershipCreateStatus.Success);

    AccountController controller = new AccountController();
    controller.FormsService = formsService.Object;
    controller.MembershipService = membershipService.Object;

    controller.Register(newUser);

    formsService.Verify(x => x.SignIn("John", false), Times.Once());
} 

这里的关键是Verify 方法。它的工作原理就像断言一样。在本例中,您要验证方法 SignIn 是否仅被调用一次。这是如何使用模拟来检查帐户控制器是否按预期工作的示例。

Ok, here is a good test: When you register a new user, you want to make sure that he will be automatically signed in the site, so he doest not need to type his username and password again.

The test would be something like this:

public void AutomaticallySignedInAfterRegistering()
{
    var membershipService = new Mock<IMembershipService>();
    var formsService = new Mock<IFormsAuthenticationService>();
    RegisterModel newUser = new RegisterModel();
    newUser.UserName = "John"
    newUser.Email = "[email protected]"
    newUser.Password = "p@ss";
    newUser.ConfirmPassword = "p@ss";

    membershipService.Setup(x => x.CreateUser("John", "p@ss", "[email protected]")).Returns(MembershipCreateStatus.Success);

    AccountController controller = new AccountController();
    controller.FormsService = formsService.Object;
    controller.MembershipService = membershipService.Object;

    controller.Register(newUser);

    formsService.Verify(x => x.SignIn("John", false), Times.Once());
} 

The key here is the Verify method. It works just like an Assert. In this case you are verifing that the method SignIn was called exactly once. This is an example of how to use mocks to check if the Account Controller is working as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文