chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi !!

We are using MVP (WCSF) framework for our view. Our view implements an interface and lot of them don't have a get on them.

i.e. IAddProduceView
{
bool AddProductAllow { set; }
.......

}

Now in presenter we set value for this:

view.AddProductAllow = false (or some condition which evaluates to true/false)

We use NaturalMock:

I want confirm that view.AddProductAllow is true or false.

Since I don't have "get", how do I evaluate value of AddProductAllow?

Thanks,
asked by love_luv (1.7k points)

6 Answers

0 votes
This is very easy to do using expectations that can be verified with Typemock Isolator. Here is a sample:

public interface ISomeInterface
{
  bool BooleanProperty { set; }
}

public class InterfaceConsumer
{
  private ISomeInterface _ctorParam;
  
  public InterfaceConsumer(ISomeInterface ctorParam)
  {
    this._ctorParam = ctorParam;
  }
  
  public void MethodWithParam(ISomeInterface methodParam)
  {
    methodParam.BooleanProperty = true;
  }
  
  public void MethodAllInternal()
  {
    this._ctorParam.BooleanProperty = true;
  }
}

[TestFixture]
[VerifyMocks]
public class MyTestFixture
{
  [Test]
  public void TestMethodWithParam()
  {
    ISomeInterface methodParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using(RecordExpectations r = RecorderManager.StartRecording())
    {
      methodParam.BooleanProperty = true;
      r.CheckArguments();
    }
    
    InterfaceConsumer consumer = new InterfaceConsumer(null);
    consumer.MethodWithParam(methodParam);
  }

  [Test]
  public void TestMethodAllInternal()
  {
    ISomeInterface ctorParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using(RecordExpectations r = RecorderManager.StartRecording())
    {
      ctorParam.BooleanProperty = true;
      r.CheckArguments();
    }
    
    InterfaceConsumer consumer = new InterfaceConsumer(ctorParam);
    consumer.MethodAllInternal();
  }
}


In the above examples, you can see how I have an interface with just a setter, and a couple of different ways to get the dependency in there - in one case, a method that takes a parameter; in another case, a constructor that gets it passed in.

In either case, the way you test it is by setting expectations in the recorder block - you'll see that I'm calling "CheckArguments" to verify the value that the property is being set to as soon as the mocks get verified (via the VerifyMocks attribute).

If you have a factory or some other mechanism that's inserting the dependency (maybe you're calling a static method to generate the instance of the interface you're using, etc.) then it just means a couple of additional mocks to set up - you can mock the return value of the static factory method to return your mock interface implementation, for example.

In any case, it's the "CheckArguments" that does the magic.
answered by tillig (6.7k points)
0 votes
Thanks for the example Travis, accurate as ever :)

love_luv, may I recommend using the AAA API? We released this with version 5.0 not long ago and it should create more readable tests.

In your case you can use our verification API to verify the property set with the expected value:

// create fake instance of the view implementing the interface
MyView fake = Isolate.Fake.Instance<MyView>(Members.CallOriginal);

// call the method under test 
fake.MethodUnderTest();

// verify the AddProductAllow property was set to be true
Isolate.Verify.WasCalledWithExactArguments(() => fake.AddProductAllow = true);


Go ahead and give it a try.

Thanks,
Doron
Typemock Support Team
answered by doron (17.2k points)
0 votes
:arrow: using the new API and Travis example, the test will be:

[Test,Isolated]
public void TestMethodAllInternal()
{
   ISomeInterface view = Isolate.Fake.Instance<ISomeInterface>(Members.ReturnRecursiveFakes);
   
   InterfaceConsumer consumer = new InterfaceConsumer(view);
   consumer.MethodAllInternal();

   // verify the AddProductAllow property was set to true
   Isolate.Verify.WasCalledWithExactArguments(() => view.AddProductAllow = true); 
} 
answered by eli (5.7k points)
0 votes
Nice. This works, but I have another issue now:

lets say in following method:

public void SomeMethod()
{
// Initialize Bool Property
BooleanProperty = true;

if (Condition X)
BooleanProperty = false;

if (Condition Y)
BooleanProperty = true;
}

Now if I use Check Argument, I will have to perform check twice assuming I am satisfying Condition X or Condition Y. Otherwise Typemock tells me "there is one more expected call".

I don't want to check argument for initialize

I want to check arguments for Condition X or Condition Y.

Thanks,
This is very easy to do using expectations that can be verified with Typemock Isolator. Here is a sample:

public interface ISomeInterface
{
  bool BooleanProperty { set; }
}

public class InterfaceConsumer
{
  private ISomeInterface _ctorParam;
  
  public InterfaceConsumer(ISomeInterface ctorParam)
  {
    this._ctorParam = ctorParam;
  }
  
  public void MethodWithParam(ISomeInterface methodParam)
  {
    methodParam.BooleanProperty = true;
  }
  
  public void MethodAllInternal()
  {
    this._ctorParam.BooleanProperty = true;
  }
}

[TestFixture]
[VerifyMocks]
public class MyTestFixture
{
  [Test]
  public void TestMethodWithParam()
  {
    ISomeInterface methodParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using(RecordExpectations r = RecorderManager.StartRecording())
    {
      methodParam.BooleanProperty = true;
      r.CheckArguments();
    }
    
    InterfaceConsumer consumer = new InterfaceConsumer(null);
    consumer.MethodWithParam(methodParam);
  }

  [Test]
  public void TestMethodAllInternal()
  {
    ISomeInterface ctorParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using(RecordExpectations r = RecorderManager.StartRecording())
    {
      ctorParam.BooleanProperty = true;
      r.CheckArguments();
    }
    
    InterfaceConsumer consumer = new InterfaceConsumer(ctorParam);
    consumer.MethodAllInternal();
  }
}


In the above examples, you can see how I have an interface with just a setter, and a couple of different ways to get the dependency in there - in one case, a method that takes a parameter; in another case, a constructor that gets it passed in.

In either case, the way you test it is by setting expectations in the recorder block - you'll see that I'm calling "CheckArguments" to verify the value that the property is being set to as soon as the mocks get verified (via the VerifyMocks attribute).

If you have a factory or some other mechanism that's inserting the dependency (maybe you're calling a static method to generate the instance of the interface you're using, etc.) then it just means a couple of additional mocks to set up - you can mock the return value of the static factory method to return your mock interface implementation, for example.

In any case, it's the "CheckArguments" that does the magic.
answered by love_luv (1.7k points)
0 votes
Nice. This works, but I have another issue now:

lets say in following method:

public void SomeMethod()
{
// Initialize Bool Property
BooleanProperty = true;

if (Condition X)
BooleanProperty = false;

if (Condition Y)
BooleanProperty = true;
}

Now if I use Check Argument, I will have to perform check twice assuming I am satisfying Condition X or Condition Y. Otherwise Typemock tells me "there is one more expected call".

I don't want to check argument for initialize

I want to check arguments for Condition X or Condition Y.

Thanks,
This is very easy to do using expectations that can be verified with Typemock Isolator. Here is a sample:

public interface ISomeInterface
{
  bool BooleanProperty { set; }
}

public class InterfaceConsumer
{
  private ISomeInterface _ctorParam;
  
  public InterfaceConsumer(ISomeInterface ctorParam)
  {
    this._ctorParam = ctorParam;
  }
  
  public void MethodWithParam(ISomeInterface methodParam)
  {
    methodParam.BooleanProperty = true;
  }
  
  public void MethodAllInternal()
  {
    this._ctorParam.BooleanProperty = true;
  }
}

[TestFixture]
[VerifyMocks]
public class MyTestFixture
{
  [Test]
  public void TestMethodWithParam()
  {
    ISomeInterface methodParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using(RecordExpectations r = RecorderManager.StartRecording())
    {
      methodParam.BooleanProperty = true;
      r.CheckArguments();
    }
    
    InterfaceConsumer consumer = new InterfaceConsumer(null);
    consumer.MethodWithParam(methodParam);
  }

  [Test]
  public void TestMethodAllInternal()
  {
    ISomeInterface ctorParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using(RecordExpectations r = RecorderManager.StartRecording())
    {
      ctorParam.BooleanProperty = true;
      r.CheckArguments();
    }
    
    InterfaceConsumer consumer = new InterfaceConsumer(ctorParam);
    consumer.MethodAllInternal();
  }
}


In the above examples, you can see how I have an interface with just a setter, and a couple of different ways to get the dependency in there - in one case, a method that takes a parameter; in another case, a constructor that gets it passed in.

In either case, the way you test it is by setting expectations in the recorder block - you'll see that I'm calling "CheckArguments" to verify the value that the property is being set to as soon as the mocks get verified (via the VerifyMocks attribute).

If you have a factory or some other mechanism that's inserting the dependency (maybe you're calling a static method to generate the instance of the interface you're using, etc.) then it just means a couple of additional mocks to set up - you can mock the return value of the static factory method to return your mock interface implementation, for example.

In any case, it's the "CheckArguments" that does the magic.
answered by love_luv (1.7k points)
0 votes
Hi,

I'm not sure i understood you correctly, but according to what I think look at the following example:
Ive added the following method to InterfaceConsumer:
public void SomeComplexMethod()
{
    //init
    _ctorParam.BooleanProperty = true; //this call we dont care about

    if (true)
        _ctorParam.BooleanProperty = false; //this we want to verify
    if (true)
        _ctorParam.BooleanProperty = true; //we also want to verify this
}


as you can see there are 3 calls to the property set and i would like to make sure that only the last 2 have the correct arguments.
here is how the test would be written:
[TestMethod]
public void TestSeveralCallToSet()
{
    ISomeInterface methodParam = RecorderManager.CreateMockedObject<ISomeInterface>();
    using (RecordExpectations r = new RecordExpectations())
    {
        methodParam.BooleanProperty = true;
        methodParam.BooleanProperty = false;
        r.CheckArguments();
        methodParam.BooleanProperty = true;
        r.CheckArguments();
    }
    InterfaceConsumer consumer = new InterfaceConsumer(methodParam);
    consumer.SomeComplexMethod();
}

:!: the key thing to understand here is that the statement: r.CheckArguments(); only apply to the previous recorded expectation. and not to all calls.

I hope this is what you intended if not let me know.
answered by lior (13.2k points)
...