简体   繁体   English

Asp.net MVC集成测试和websecurity

[英]Asp.net MVC integration test and websecurity

I am developing an ASP.Net MVC 4 website and I would like to test it. 我正在开发一个ASP.Net MVC 4网站,我想测试它。 In the controller that I would like to test, I call 在我想测试的控制器中,我打电话

WebSecurity.CreateUserAndAccount WebSecurity.CreateUserAndAccount

and I have some logic around this call that I would like to test. 我有一些关于这个电话的逻辑,我想测试一下。 So I test the whole action (and in the future, I will have to do it more than once). 所以我测试了整个动作(将来,我将不得不多次这样做)。

So this is my test 所以这是我的考验

[TestMethod]
        public void RegisterUser()
        {
            // Arrange
            var controller = new UserController("TestConnection");

            // Act
            var user= new UserModel()
            {
                MailAddressTemp = "dummyuser@gmail.com",
                Password = "password",
                ConfirmPassword = "password",
                FirstName = "firstname",
                LastName = "lastname",
            };
            TestHelper.ValidateViewModel(user, controller);
            if (!controller.ModelState.IsValid)
                Assert.Inconclusive("Model should be valid!");
            controller.Register(user);
            using (var context = new PatientDbContext("TestConnection"))
            {
                var userProfile = context.UserProfiles.FirstOrDefault(x => x.MailAddress == "dummyuser@gmail.com");
                Assert.IsNotNull(userProfile);
                Assert.AreEqual("dummyuser@gmail.com", WebSecurity.CurrentUserName);
                Assert.IsTrue(WebSecurity.UserExists("dummyuser@gmail.com"));
                WebSecurity.Logout();
                var islogged = WebSecurity.Login("dummyuser@gmail.com", "password");
                Assert.IsTrue(islogged);
            }
        }

My problem is that I have an exception when I want to use 我的问题是,当我想使用时,我有一个例外

WebSecurity.CreateUserAndAccount WebSecurity.CreateUserAndAccount

This is the exception 这是个例外

To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider" 要调用此方法,“Membership.Provider”属性必须是“ExtendedMembershipProvider”的实例

I will need to use WebSecurity in my future tests. 我将来需要在未来的测试中使用WebSecurity。 I'm from the WPF world and in my integration test, I was able to start an application container and test it from the unit test solution. 我来自WPF世界,在我的集成测试中,我能够启动一个应用程序容器并从单元测试解决方案中测试它。 Here, it looks like the web application is not started when I run the tests and I don't know how to do it. 在这里,看起来我运行测试时没有启动Web应用程序,我不知道该怎么做。

My questions: 我的问题:

How can I use WebSecurity in my test solution and solve the exception? 如何在我的测试解决方案中使用WebSecurity并解决异常?

How can I start my application so that all objects are loaded like in a live application and test my actions from the code in the unit tests? 如何启动我的应用程序以便像在实时应用程序中一样加载所有对象并从单元测试中的代码测试我的操作?

I don't want a browser. 我不想要浏览器。 I want to be able to do something like 我希望能够做类似的事情

StartApp()
var result = MyController.Action(params)//My action uses WebSecurity for instance
Assert.IsTrue(something from result)

Any help will be helpful! 任何帮助都会有所帮助! Thank you! 谢谢!

You'd better to create decorator around static methods of WebSecurity class. 你最好围绕WebSecurity类的静态方法创建装饰器。 You will be able to mock it and isolave your controller core. 您将能够模拟它并隔离您的控制器核心。 For example: 例如:

public interface IWebSecurity
{
    bool CreateUserAndAccount(....);
}

public class MyWebSecurity: IWebSecurity
{
    public bool CreateUserAndAccount(...)
    {
        WebSecurity.CreateUserAndAccount(...);
    }
}

in your test code you will be able to do (using Moq test framework): 在您的测试代码中,您将能够做到(使用Moq测试框架):

var webSecurity=new Mock<IWebSecurity>();
var controller = new UserController("TestConnection", webSecurity.Object);

I found part of the solution here . 我在这里找到了部分解决方案。 I needed to add this in my app.config in my test solution 我需要在我的测试解决方案的app.config中添加它

<system.web>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <clear />
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear />
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>

Before my tests, I had to call 在我的测试之前,我不得不打电话

    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "MailAddress", autoCreateTables: true);

You must add WebMatrix.webData and WebMtrix.Data in reference in your test project and set "Copy local" to true 您必须在测试项目中的引用中添加WebMatrix.webData和WebMtrix.Data,并将“Copy local”设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM