2

I am trying to test my real data by actually hitting the database. I am actually testing my Repository classes. Here is an example of what I am doing;

/// <summary>
/// Summary description for Country
/// </summary>
[TestClass]
public class Country {

    public Country() {
        _countryRepo = new CountryRepository();
    }

    private ICountryRepository _countryRepo;

    [TestMethod]
    public void db_should_return_at_least_one_country_as_approved_all() {

        //Act
        var model = _countryRepo.GetAll();

        //Assert
        Assert.IsTrue(model.Count() >= 1);
    }

    [TestMethod]
    public void db_should_return_at_least_one_country_as_approved_true() { 

        //Act
        var model = _countryRepo.GetAll(ApprovalStatus.Approved);

        //Assert
        Assert.IsTrue(model.Count() >= 1);
    }

    [TestMethod]
    public void db_should_return_Turkey_as_country_name_by_id() {

        //Act
        var model = _countryRepo.GetSingle(1000);

        //Assert
        Assert.AreEqual<string>("Turkey", model.CountryName);

    }

    [TestMethod]
    public void db_should_return_Turkey_as_country_name_by_countryISO3166Code() {

        //Act
        var model = _countryRepo.GetSingle("TR");

        //Assert
        Assert.AreEqual<string>("Turkey", model.CountryName);

    }

    [TestMethod]
    public void db_should_return_Turkey_as_country_name_by_GUID() {

        //Act
        var model = _countryRepo.GetSingle(Guid.Parse("9AF174A6-D0F7-4393-AAAD-B168BADEDB30"));

        //Assert
        Assert.AreEqual<string>("Turkey", model.CountryName);

    }

}

This works pretty well for my needs but wondering if I am doing it right by the book. Is there any other patterns that I really should be following here. I do not want to fake my data, my real intense here is to test my DAL and real production data.

tugberk
  • 57,477
  • 67
  • 243
  • 335

3 Answers3

2

Your tests will fail if someone else (or even you) go to your database and create a new approved country or change your country name. You are going to think: "WTH is wrong with my repository, why is it not working as expected?" But yeah, the problem isn't with the repository.

When I write tests that hit the database I like to create the DB and load default values at startup and destroy then all right after that. I'm not sure if this is the best alternative, but it works pretty well. The problem with this approach is that it's slower and there is more to code.

goenning
  • 6,514
  • 1
  • 35
  • 42
  • yes, you are right, it will break if someone else will go to table and change the country name Turkey to something else. But my intense here is to test if my EF code is working or not. So, you suggest to load all the values into variables and then use them later? what differences could it bring? – tugberk Aug 29 '11 at 11:49
  • Not variables. I run a SQL at startup that creates the DB and the data. After that, my tests are run and it will check if the Repository is returning the expected values. I'm sure that the data will be there and I know what to expected because I've wrote then at the SQL (that is in the test code). The difference is that you're not depending so much on the current state of the database. If some of your tests changes the state of a row, you'll need to rollback it because the next time you run it, the test may fail. – goenning Aug 29 '11 at 12:08
  • all right. So making a copy of the db and running it in local would also work with fake data, right? – tugberk Aug 30 '11 at 10:08
0

For the requirement at hand, i.e. data that is really static, and should not be tampered with, I'd say this is a valid approach.

I would recommend to write data-driven tests however, instead of repeating the same test case for each country.

Community
  • 1
  • 1
jeroenh
  • 26,362
  • 10
  • 73
  • 104
0

You should use static database just for testing and your methods testing GetAll should assert against real expected count. How do you know that it really returned what you expected if you just assert that it returned at least one record? You should even go through result set and test that all records satisfy the condition (but it is usually used for more complicated conditions).

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670