Reputation: 6607
I've seen several questions here dealing with similar problems but none of them have helped me.
I am using NUnit with VS 2010 in an MVC 3 project. I have a Tests project and am writing my first tests Evar! :-)
Aren't you proud I finally am getting to it?!
Here is the error I am getting
Microsoft.Practices.ServiceLocation.ActivationException : Activation error occured while trying to get instance of type Database, key "MyConnection" ----> Microsoft.Practices.Unity.ResolutionFailedException : Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "MyConnection". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
I have configured the web.config
with all this and the data returns perfectly when not running in a test so I know it's not the config that is dying per ce, it's just dying when used with NUnit.
Here is my connection info:
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
...
<dataConfiguration defaultDatabase="MyConnection" />
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=MyServerName;Initial Catalog=MyDB;user id=MyUser;password=MyPassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
I have installed the DaaB using NuGet and also doing a clean manual reference. Either way, the data loads fine in normal usage but in testing it dies at this line:
var database = DatabaseFactory.CreateDatabase("MyConnection");
In this method
public IEnumerable<SchoolSearchResultsDTO> Find(SchoolSearchInputDTO dto) {
List<SchoolSearchResultsDTO> fullList;
var database = DatabaseFactory.CreateDatabase("MyConnection");
using (var command = database.GetStoredProcCommand("dbo.usp_School_SearchBySchoolName")) {
database.AddInParameter(command, "@I_strSchoolName", DbType.String, dto.SearchTerm);
database.AddInParameter(command, "@I_intNumberOfRecords", DbType.Int32, dto.MaxSearchResults);
using (var reader = database.ExecuteReader(command)) {
fullList = new List<SchoolSearchResultsDTO>();
while (reader.Read()) {
var fullRecord = new SchoolSearchResultsDTO();
fullRecord.SchoolID = reader.GetInteger("SchoolId");
fullRecord.SchoolName = reader.GetString("SchoolName");
fullRecord.IsDetailedDisplayMode = reader.GetBoolean("IsDetailedDisplayMode");
fullList.Add(fullRecord);
}
reader.Close();
}
}
return fullList;
}
All the other posts talk about misconfiguration etc.. I'm pretty sure I'm configured correctly otherwise I wouldn't get data under normal usage. So it has to do with NUnit and DaaB together.
Any bright ideas? :-) Thanks all!
Upvotes: 2
Views: 1464
Reputation: 2456
Agree with @Jesse regarding mocking dependencies. If you're not isolating the sut then its not a unit test. (recommended reading Art of UnitTesting)
So moving on from that, you could say you are writing integrations tests.
Without looking at your exact setup, it's hard to tell what the issue is. One thing you mentioned was you have "configured your web.config
".
But have you setup a config file in your test project? The config will be loaded from your unit test project, not the MVC project.
Upvotes: 4
Reputation: 8393
Generally it is good practice to avoid hitting a database directly within a unit test. I would recommend setting aside sometime to read about mocking in regards to creating valuable and reliable unit tests. With mocking, you can inject into the related controller a mocked service layer, which would be setup to return your expected database data.
Within your test you would create a mock of that service layer. For an example utilizing moq (my mocking framework preference):
// create your expected data
var YourExpectedData = new IList<SchoolSearchResultsDTO>();
// .... add your expected information
//create & setup mock
var _service = new Mock<IYourService>();
_service.Setup(service => service.Find(It.IsAny<SchoolSearchInputDTO>())).Returns(YourExpectedData);
// create your controller
var controller = new YourCountroller(_service.object())
From this point on your unit test is quite straight forward. Any time the method find is called within the various code your testing the mock kicks in and returns your expected data.
Upvotes: 2