- I created a simple Asp.net MVC 4 application
- I created the NUnit test project using the visula studio 2010 extension
- I add a couple of controllers (with the models and views) and the website works fine. It works with Sql Server CE
Now I need to test a controller. I wrote this simple class:
namespace SkateboardShop.Tests.Controllers
{
[TestFixture]
class BrandControllerTest
{
[Test]
public void TestDetailsView()
{
var controller = new BrandController();
var result = controller.Details(2) as ViewResult;
Assert.AreEqual("Details", result.ViewName);
}
}
}
When I run this test the NUnit return me this error:
SkateboardShop.Tests.Controllers.BrandControllerTest.TestDetailsView:
System.Data.ProviderIncompatibleException : An error occurred while
getting provider information from the database. This can be caused by
Entity Framework using an incorrect connection string. Check the inner
exceptions for details and ensure that the connection string is
correct. —-> System.Data.ProviderIncompatibleException : The
provider did not return a ProviderManifestToken string. —->
System.Data.SqlClient.SqlException : A network-related or
instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL Server is configured to
allow remote connections. (provider: SQL Network Interfaces, error: 26
– Error Locating Server/Instance Specified)
Sure, where the test project get the data provider information?
I add into the app.config file (of the test project) the connection string like this:
<connectionStrings>
<add name="SkateboardEntities" connectionString="Data Source=d:\Basic\project\asp\SkateboardShop\SkateboardShop\App_Data\SkateboardShop.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
But nothing to do.
Since I’m new in MVC and NUnit, I’m asking, in the first place, if the Unit Test should test real database data and if it’s a good way, how to do that.
Thanks.
In my opinion – you should be having a mock repository/persistance layer for testing.
In that way – you can have a predictable result set to work against & write tests to.
An example of this could be your controllers accepting an instance of a repository with code like:
You can use any DI + Mock framework to inject the IBrandRepository for unit testing
But, there are situations where the mock is not sufficient & you do need a physical database for testing.
In such cases, I would recommend to have a test database which has the same structure, but test data which can be re-seeded/initialized as needed for your unit testing.
In this case, you could point your config file to the test database for testing & some sort of script or DbInitializer to initialize the database back to the original state etc.
HTH.