guazz
guazz

Reputation: 21

Integration Test Example With Rhino Mocks

I have two classes. I would like to verify that the properties are called on one of the classes.

public classA  
{  
    public IBInterface Foo {get;set;}  
    public LoadData()    
    {  
      Foo.Save(1.23456, 1.23456);  
    }  
}  

public classB : IBInterface   
{  
    public decimal ApplePrice {get; set;}    
    public decimal OrangePrice {get;  set;}    

    public void Save(decimal param1, decimal param2)  
    {  
        this.ApplePrice = param1;  
        this.OrangePrice = param2;  
    }
}  

I would like to use Rhino Mocks(AAA syntax) to verify that ApplePrice and OrangePrice were set correctly.

I assume I should begin like so but how do I verify that ApplePrice and OrangePrice have been set?

var mockInterfaceB = mockery.DynamicMock();
ClassA a = new ClassA();
a.Foo = mockInterfaceB;
a.LoadData();

Upvotes: 0

Views: 1690

Answers (1)

Jay
Jay

Reputation: 57919

Your question title asks for an integration test, not a unit test, so it seems you ought not mock or stub classB, because a big part of what you're trying to verify is the behaviour of exactly that class. Stubbing it would defeat that purpose; RhinoMocks has no place here.

// arrange
var expected = 1.23456;
var class_b = new classB();
var class_a = new classA{ Foo = class_b };

// act
a.LoadData();

// assert
Assert.AreEqual(expected, class_b.ApplePrice);
Assert.AreEqual(expected, class_b.OrangePrice);

If what you really want is a unit test, then your first task is to identify the unit of functionality that you are testing.

From your description, it sounds like you want to test two different things:

  1. that calling LoadData() calls Save(1.23456, 1.23456)
  2. that calling Save(...) sets values on the properties ApplePrice and OrangePrice.

Here is how I'd write those tests:

1

note: I would pass two different values to the Save() method to be sure that the right parameter is being assigned to the right property

 // arrange
 var expected_apple = 1.23456m;
 var expected_orange = 6.54321m;

 var b_mock = MockRepository.GenerateStub<IBInterface>();
 b_mock.Stub(x => x.Save(Arg<decimal>.Is.Anything, Arg<decimal>.Is.Anything))
 .WhenCalled(x =>
 {
    b_mock.ApplePrice = (decimal) x.Arguments[0];
    b_mock.OrangePrice = (decimal) x.Arguments[1];
 });

 var sut = new classA{ Foo = b_mock };

 // act
 sut.LoadData();

 // assert
 b_mock.AssertWasCalled(x => x.Save(expected_apple, expected_orange),
    options => options.Repeat.Once());
 Assert.AreEqual(expected_apple, b_mock.ApplePrice);
 Assert.AreEqual(expected_orange, b_mock.OrangePrice);

2

note: you don't need classA here at all; you're only testing the functionality of the Save() method on classB

// arrange
var apple = 1.23456m;
var orange = 6.54321m;

var sut = new classB();

// act
sut.Save(apple, orange);

// assert
Assert.AreEqual(apple, sut.ApplePrice);
Assert.AreEqual(orange, sut.OrangePrice);

edit in response to comment:

To verify the behaviour of this method on classA:

LoadData() { Foo.ApplePrice = -1; Foo.OrangePrice = -2; }

test:

// arrange
var apple = -1m;
var orange = -2m;

var stub_b = MockRepository.GenerateStub<classB>();
var sut = new classA{ Foo = stub_b };

// act
sut.LoadData();

// assert
Assert.AreEqual(apple, stub_b.ApplePrice);
Assert.AreEqual(orange, stub_b.OrangePrice);

Upvotes: 2

Related Questions