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,
I'm trying to write a test that make sure that I do certain calls to certain objects. The test should fail
(a) If I do not do all calls in my object under test and
(b) If I do more calls in my object under test then I expected

(a) is pretty easy. MockManager.Verify will check for that, right?
so now for (b).

See following code

class ClassA
{
private ClassB b;
private ClassC c;

public ClassA()
{
b = new ClassB();
c = new ClassC();
b.Subscribe(c);
}

}

class ClassB
{
public ClassB()
{

}

public void Subscribe(ClassC c)
{
}
}

class ClassC
{
public ClassC()
{
}
}

[TestFixture]
public class TestFix
{
[Test]
public void Test_Init()
{
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.DefaultBehavior.CheckArguments();

ClassB b = new ClassB();
ClassC c = new ClassC();
//b.Subscribe(c);
}

ClassA a = new ClassA();

MockManager.Verify();

}
}

In the c'tor of ClassA, I call b.Subscribe(c). My test still passes, even though I did not specify it in my test.
Is there a way to fail a test if let's say any calls are made to b that are not expected?
asked by Step (3.4k points)

2 Answers

0 votes
Hi
You can do that by using the DefaultBehavior.Strict property
Try this:
class ClassA
{
   private ClassB b;
   private ClassC c;

   public ClassA()
   {
      b = new ClassB();
      c = new ClassC();
      b.Subscribe(c);
   }

}

class ClassB
{
   public ClassB()
   {

   }

   public void Subscribe(ClassC c)
   {
   }
}

class ClassC
{
   public ClassC()
   {
   }
}

[TestFixture]
public class TestFix
{
   [Test]
   public void Test_Init()
   {
      using (RecordExpectations recorder = RecorderManager.StartRecording())
      {
         recorder.DefaultBehavior.CheckArguments();
         recorder.DefaultBehavior.Strict = StrictFlags.AllMethods;

         ClassB b = new ClassB();
         ClassC c = new ClassC();
         //b.Subscribe(c);
      }

      ClassA a = new ClassA();
      MockManager.Verify();

   }
}  


Note that in this example StrictFlags.InstanceMethods would work as well.
So choose what suits you the best.
answered by ohad (35.4k points)
0 votes
Thanks,
that works great.
answered by Step (3.4k points)
...