mardi 2 mai 2017

Should be database mocks centralized?

I am new to testing and i do not have experience with mocks, but i watch some videos and have a question.

For example, i have this kind of test, where i test if the CustomerController.DoSomething() method, which is internally calling CustomerRepository.GetCustomer() is rendering the right view.

[TestMethod]
public void ShouldRenderDefaultView()
{
    // Arrange
    Mock<ICustomerRepository> mock = new Mock<ICustomerRepository>();
    mock.Setup(m => m.GetCustomer).Returns(new Customer {
                    Id = 5,
                    FirstName = "William",
                    LastName = "Shakespear"
                    Profession = "Artist"
                    NumberOfWomen = 3
                    NumberOfMeetingsPerWeek = 99999999
                });


    CustomerController controller = new CustomerController(mock.Object);
    // Action
    var result = controller.DoSomething() as ViewResult;
    // Assert
    Assert.AreEqual("DoSomething", result.ViewName);
}

the issue i have is, that for each test everyone can mock/fake database data but if the real query output will change (for example removing a column Profession or adding some database constraint that NumberOfMeetingsPerWeek cant be more than 7) then there will be a need to update many fake implementations.

so the question is, what about to have predefined mocks for example

    class WilliamShakespearCustomer : ICustomer
    {
        public void WilliamShakespearCustomer()
        {
           FirstName = "William";
           LastName = "Shakespear";
           // etc...
        }

      public string FirstName;
      public string LastName;
      // etc ...
    }

and the new test can now use this predefined mock customer and im sure that every test use this customer configuration which is valid against database.

[TestMethod]
    public void ShouldRenderDefaultView()
    {
        // Arrange
        Mock<ICustomerRepository> mock = new Mock<ICustomerRepository>();
        mock.Setup(m => m.GetCustomer).Returns(new WilliamShakespearCustomer()});

        CustomerController controller = new CustomerController(mock.Object);
        // Action
        var result = controller.DoSomething() as ViewResult;
        // Assert
        Assert.AreEqual("DoSomething", result.ViewName);
    }

Is this valid concern or it is not a real problem iam thinking about ?

Aucun commentaire:

Enregistrer un commentaire