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);
}
Nice practice is testing units in isolation. That also means abstracting your environment – database, file system (including configuration files), etc. So, extract
TwitterSettingsinterface from yourTwitterSettingsclass:Implement this interface by your settings:
And inject this dependency to controller:
BTW abstracting of environment makes tests run faster, and your tests will fail only if your SUT logic is broken, not if database server not responding, or web.config is not found.