Skip to content

How to verify methods and properties

Consider the following service:

c#
public class Consumer{
  private readonly IEffect _effect;

  public Consumer(IEffect effect){
    _effect = effect;
  }

  public void Apply(){
    _effect.Apply(this);
  }
}
public class Consumer{
  private readonly IEffect _effect;

  public Consumer(IEffect effect){
    _effect = effect;
  }

  public void Apply(){
    _effect.Apply(this);
  }
}

When we now want to validate if the Apply method of the IEffect interface is called, we need access to the effect object.

To validate that the Apply method is called, the ItemContext can be used. The context can be used to get dependencies which are resolved and set by the ItemBuilder. But also for validating if a method or property was called. To validate if a method was called the Verify method can be used.

c#
var consumer = new MyConsumerBuilder()
    .With(p => p.Ctor.effect.Stub<IEffect>())
    .Build(out var context);

consumer.Apply();

// to verify that the Apply method is called we select the Apply method.
// and the verify it with the Called method.
context.Verify(p => p.Ctor.effect.Apply)
    .Called();
var consumer = new MyConsumerBuilder()
    .With(p => p.Ctor.effect.Stub<IEffect>())
    .Build(out var context);

consumer.Apply();

// to verify that the Apply method is called we select the Apply method.
// and the verify it with the Called method.
context.Verify(p => p.Ctor.effect.Apply)
    .Called();

Note

The context can only be retrieved form a Custom Item Builder.

Parameter verification

It is also possible to validate if a method is called with the right parameter. For this, TWIZZAR provides methods which get automatically generated.

c#
public interface IStorage
{
    void Store(IIngredient ingredient);
    IIngredient Take(string ingredientName);
}

var storage = new MyIStorageBuilder()
    .Build(out var context);
public interface IStorage
{
    void Store(IIngredient ingredient);
    IIngredient Take(string ingredientName);
}

var storage = new MyIStorageBuilder()
    .Build(out var context);

To verify IStorage.Take is called where the ingredientName is "MyPotion":

c#
context.Verify(p => p.Take)
    .WhereIngredientNameIs("MyPotion")
    .Called(1);
context.Verify(p => p.Take)
    .WhereIngredientNameIs("MyPotion")
    .Called(1);

It is also possible to use a predicate to check for parameters. Here we check if the IStorage.Store method was called and the ingredient provided has the Name "Mushroom".

c#
context.Verify(p => p.Store)
    .WhereIngredientIs(ingredient => ingredient.Name == "Mushroom")
    .Called(1);
context.Verify(p => p.Store)
    .WhereIngredientIs(ingredient => ingredient.Name == "Mushroom")
    .Called(1);

If we set up the IStorage.Take method to return a specific ingredient and want to check if IStorage.Store is called and the set-up ingredient is provided as a parameter, we could do the following:

c#
var ingredient = context.Get(p => p.Take);
context.Verify(p => p.Store)
    .WhereIngredientIs(ingredient)
    .Called(1);
var ingredient = context.Get(p => p.Take);
context.Verify(p => p.Store)
    .WhereIngredientIs(ingredient)
    .Called(1);

But TWIZZAR also provides a Verify method which can handle this in one statement.

c#
context.Verify(p => p.Store)
    .WhereIngredientIs(p => p.Take)
    .Called(1);
context.Verify(p => p.Store)
    .WhereIngredientIs(p => p.Take)
    .Called(1);

Validate the count of calls

To validate the count of calls the Called method can be used. The Called method accepts an integer to set the call count. The Called method can also be used with the CalledAtLeastOnce method to validate that the method was called at least once.

c#
// called exactly twice
context.Verify(p => p.Store)
    .Called(2);

// called at least once
context.Verify(p => p.Store)
    .CalledAtLeastOnce();
// called exactly twice
context.Verify(p => p.Store)
    .Called(2);

// called at least once
context.Verify(p => p.Store)
    .CalledAtLeastOnce();

Property verification

The verification of a property works similar to the method verification, use the Verify method on the ItemContext to select a property. Afterward use the Get or Set method to select the getter or setter of the property.

Verify the getter of MyProperty is called once:

c#
context.Verify(p => p.MyProperty)
    .Get()
    .Called(1);
context.Verify(p => p.MyProperty)
    .Get()
    .Called(1);

Verify the setter of MyProperty is called once:

c#
context.Verify(p => p.MyProperty)
    .Set()
    .Called(1);
context.Verify(p => p.MyProperty)
    .Set()
    .Called(1);

Verify the setter of MyProperty is called once and the set value is "MyValue":

c#
context.Verify(p => p.MyProperty)
    .Set("MyValue")
    .Called(1);
context.Verify(p => p.MyProperty)
    .Set("MyValue")
    .Called(1);