测试在Windows身份验证方法,行动 [英] Testing a windows authenticated Action method

查看:231
本文介绍了测试在Windows身份验证方法,行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写单元测试的操作方法,并得到以下错误的操作方法。

I am writing unit tests for the action methods and get the following error in the Action method.

类型的'System.InvalidOperationException'例外发生在
  EntityFramework.dll而不是在用户code处理

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

其它信息:没有实体框架提供发现的
  ADO.NET提供与固定名称System.Data.SqlClient的。使
  确保供应商是注册在的的EntityFramework部分
  应用程序配置文件。

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

public ActionResult Index()
    {
        var user = db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name);
        ViewBag.UserName = user.UserName;
        return View("Index");
    }

测试方法

[TestMethod]
public void Home()
    {
        var controller = new HomeController();
        var result = controller.Index() as ViewResult;
        Assert.AreEqual("Index", result.ViewName);
    }

我如何能够通过用户的值与测试方法,以便测试不抛出任何错误。

How can i pass the value of user from the test method so that the test do not throw any errors.

推荐答案

看到你的问题已经回答了,我只是想给一些建议 - 你应该几乎从来没有尝试测试你的行动。这是不好的paractice - 难以实现(所有的嘲笑,你应该考虑),并带来了什么价值。在服务/佣工业务逻辑,而不是进行测试和验证。所以你的情况我宁愿有:

Seeing that your question has already been answered, I just want to give a bit of advice - you should almost never try to test your actions. This is is bad paractice - difficult to achieve (all the mocking you should take into consideration) and brings little value. Have your business logic in services/helpers instead and test them. So in your case I would rather have:

var user = UserService.GetUserByName(User.Identity.Name);

然后,我将有该服务方法的单元测试。

And then I will have a unit test for that service method.

这篇关于测试在Windows身份验证方法,行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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