web.config文件中的Custom Config部分会破坏我的单元测试

qin*_*126 2 c# unit-testing asp.net-mvc-3

我的单元测试工作,但在我添加一些代码以从web.config文件中的Custom Config部分获取值之后,单元测试停止工作.以下是我的代码,那些标有"// new"的行是我添加的新代码,它们打破了测试.

public partial class AccountController : Controller
{
    private readonly string _twitterConsumerKey;
    private readonly string _twitterConsumerSecret;
    private readonly string _twitterAccessToken;
    private readonly string _twitterAccessTokenSecret;

    private readonly IUserService _userService;

    public AccountController(IUserService userService)
    {
        if (userService == null)
            throw new ArgumentNullException("userService");


        _userService = userService;

        _twitterConsumerKey = TwitterSettings.Settings.ConsumerKey;  //new code
        _twitterConsumerSecret = TwitterSettings.Settings.ConsumerSecret;  //new code
        _twitterAccessToken = TwitterSettings.Settings.AccessToken;  //new code
        _twitterAccessTokenSecret = TwitterSettings.Settings.AccessTokenSecret;  //new code

    }





public class TwitterSettings : ConfigurationSection
{

    private static TwitterSettings settings = ConfigurationManager.GetSection("Twitter") as TwitterSettings;
    public static TwitterSettings Settings { get { return settings; } }

    [ConfigurationProperty("ConsumerKey", IsRequired = true)]
    public string ConsumerKey
    {
        get { return (string)this["ConsumerKey"]; }
        set { this["ConsumerKey"] = value; }
    }

    [ConfigurationProperty("ConsumerSecret", IsRequired = true)]
    public string ConsumerSecret
    {
        get { return (string)this["ConsumerSecret"]; }
        set { this["ConsumerSecret"] = value; }
    }


    [ConfigurationProperty("AccessToken", IsRequired = true)]
    public string AccessToken
    {
        get { return (string)this["AccessToken"]; }
        set { this["AccessToken"] = value; }
    }

    [ConfigurationProperty("AccessTokenSecret", IsRequired = true)]
    public string AccessTokenSecret
    {
        get { return (string)this["AccessTokenSecret"]; }
        set { this["AccessTokenSecret"] = value; }
    }


}
Run Code Online (Sandbox Code Playgroud)

当我在单元测试中调用此控制器时.我收到以下错误消息:"失败:System.NullReferenceException:对象引用未设置为对象的实例".我需要创建一个ISetting接口???

  [Test]
    public void Login_Action_Get_Returns_Login_View()
    {
        // Arrange

        var expectedViewName = "~/Views/Account/Login.cshtml";

       // it breaks here.
        _accountController = new AccountController(_userService.Object, _mappingService.Object, _authenticationService.Object);

        // Act

        var result = _accountController.Login() as ViewResult;

        // Assert

        Assert.AreEqual(expectedViewName, result.ViewName, "View name should be {0}", expectedViewName);
    }
Run Code Online (Sandbox Code Playgroud)

Ser*_*kiy 7

不错的做法是单独测试单元.这也意味着抽象你的环境 - 数据库,文件系统(包括配置文件)等.所以,TwitterSettings从你的TwitterSettings类中提取界面:

public interface ITwitterSettings
{
    string ConsumerKey { get; set; }
    string ConsumerSecret { get; set; }
    string AccessToken { get; set; }
    string AccessTokenSecret { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

通过您的设置实现此界面:

public class TwitterSettings : ConfigurationSection, ITwitterSettings
Run Code Online (Sandbox Code Playgroud)

并将此依赖项注入控制器:

public partial class AccountController : Controller
{
    private readonly IUserService _userService;
    private readonly ITwitterSettings _twitterSettings;

    public AccountController(IUserService userService, 
                             ITwitterSettings twitterSettings)
    {
        if (userService == null)
            throw new ArgumentNullException("userService");    

        _userService = userService;
        _twitterSettings = twitterSettings;
    }   
}
Run Code Online (Sandbox Code Playgroud)

BTW抽象环境使测试运行得更快,只有当您的SUT逻辑被破坏时,您的测试才会失败,而不是数据库服务器没有响应,或者找不到web.config.