Testing an ASP.NET MVC module with data

We have an application with perfect data management. We want to use the unit test application, but the developers are resistant to creating completely fake repositories due to the amount of data. I really don't blame them.

Understand that we will reorient tests to the existing application. If we begin, we will make a ton of architectural changes to facilitate module testing with fake repositories.

We would like to distribute the well-known mdf file with tests, copy it and use it to run our tests. Is there an approved methodology for this? I am familiar with investing resources in a test dll, but not with mdf's embedding - if you can even do that.


Solution (view):

I ended up getting a DataContextWrapper message from Andrew Tokeli's message about mocking Linq data contexts (http://andrewtokeley.net/archive/2008/07/06/mocking-linq-to-sql-datacontext.aspx) and created FakeDataContext.cs, which is basically a list of lists.

I wrote a really barbaric T4 template (think "select * FROM <# = table.BaseClass.QualifiedName #>") to copy data from a well-known good database to create a huge class full of things like:

List<Customer> _customers = new List<Customer>(); _customers.Add(new Customer(){CustomerId = 1, CustomerName = "ACME"}); 

and etc.

The class is 25K lines, but since t4 writes all these lines, who needs it? This allows us to make fun of only the data context, so we can test our linq against the fake context with some reasonable assurance that we received the correct requests. Initial developers put a ton of business logic in the repo, so it allows us to test the logic using well-known good data.

+6
unit-testing asp.net-mvc integration-testing mdf
source share
2 answers

Can you set up a test database on a shared server so that there is no need to deploy mdf files?

Also, can you wrap all unit tests with TransactionScope?

I used a test database in my company, which contained well-known reference data for all tests and created a base class for such integration tests:

 [TestClass] public class ServiceTest { private TransactionScope Transaction { get; set; } [TestInitialize] public virtual void TestInitialize() { Transaction = new TransactionScope(); } [TestCleanup] public virtual void TestCleanup() { Transaction.Dispose(); } } 

In each test, all changes will be rolled back, so there is no problem with the test data polluting the database.

+5
source share

Have you looked at the mocking structure? If you code is written in such a way that data dependencies can be injected into objects that you are testing on a module, then you should be able to simulate these data dependencies.

I had great success with Moq, but then from the very beginning I wrote my code with dependency injection.

0
source share

All Articles