mardi 17 février 2015

Using a base class for an integration test

My base class for an integration test:



[TestClass]
public class BaseIntegrationTest
{
protected readonly ApplicationDbContext _dbContext;

public BaseIntegrationTest()
{
_dbContext = new ApplicationDbContext("DefaultConnectionTest");
}

[ClassInitialize]
public void Initialize()
{
InsertTestData();
}

public void InsertTestData()
{
_dbContext.Users.Add(new User { Name = "John Doe" });
}
}


Here's a test class that inherits from BaseIntegrationTest:



[TestClass]
public class UserRepositoryTests : BaseIntegrationTest
{
// how to lose this method here and use the one in the BaseIntegrationTest?
//[ClassInitialize]
//public static void Setup(TestContext context)
//{
// BaseIntegrationTest test = new BaseIntegrationTest();
// test.InsertTestData();
//}

[TestMethod]
public void GetUser()
{
Filter f = new Filter()
{
Name = "John Doe"
};

UserRepository fr = new UserRepository(_dbContext);
var result = fr.GetUser(f);

Assert.IsTrue(result.Any(x => x.Name == "John Doe"));
}
}


The method decorated with the [ClassInitialize] attribute in the BaseIntegrationTest never gets executed, and because of that, my tests fail. No data gets inserted into the database.


But if I uncomment the public static void Setup(TestContext context) in the UserRepositoryTests it works.


I have a few more repositories I want to test. And I want the data being inserted into the database being done in the base class so that I can run all the tests without recreating the database for every repository I want to test.


Converting public void Initialize() into public static void Initialize(TestContext context) doesn't work either. The Initialize() wont get executed/called.


Am I missing something? Or are these limitations of unit test/integration test? I'm pretty new to unit testing/integration testing.


Aucun commentaire:

Enregistrer un commentaire