سؤال

I'm creating a methodology to write Unit Tests in my company. We are focused on testing one thing each time but in the case of checking that a base constructor receives the right parameters I'm not finding anything else rather than testing the base constructor as well.

Is there a better approach? We are using MSTest together with JustMock and Rhino so extended features are available but anyway I've not find a way. Find an example below:

public Car : Vehicle
{
    public Car() : base(4) {};
}

public Vehicle
{
    public Vehicle(int wheelNumber) {};
}

I would like to put an expectation on the base constructor parameter as I would do with a normal method.

Thanks

هل كانت مفيدة؟

المحلول

Don't expect anything about the base constructor. For your code, I would make sure that Car is a vehicle and then check that the default wheelNumber was set properly (and this only if there were a public method for knowing that).

If the constructor for Car repeated the code in Vehicle involving the wheelNumber your test should still pass. And then when someone refactors the code so that it is done properly the test should still pass. That way you know that you haven't changed your functionality.

نصائح أخرى

I'm not sure you can test a base constructor in that manner.

If you are exposing a base property perhaps you test that

Roughly:

Assert.AreEqual(car.BaseProperty,4)

Or you can test the expected behavior of a public method that acts on the data/variable, another pseudocode:

 class Vehicle
 {
    protected int  X{}
    public Vehicle(int x)
    { 
      X= x;
      ++X;
     }
 }
 car = new Car(4);
 Assert.ReturnsFive(car.DoSomething(),5)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top