my unit test works, but after I added some code to get values from Custom Config section in web.config file, unit test stops working. following is my code, those line marked with "//new" are the new code I added, and they break the test.
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; }
}
}
when I call this controller in my unit test. I got following error message: "Failed: System.NullReferenceException: Object reference not set to an instance of an object". Do I need to create a ISetting interface???
[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);
}