0

How can I write this simple Record-and-replay based test in AAA Syntax with Rhino Mocks framework?

public interface IStudentReporter
{
      void PrintStudentReport(List<IStudent> students);
      List<IStudent> GetUnGraduatedStudents(List<IStudent> students);
      void AddStudentsToEmptyClassRoom(IClassRoom classroom, List<IStudent> 
}




 [Test]
    public void PrintStudentReport_ClassRoomPassed_StudentListOfFive()
     {
        IClassRoom classRoom = GetClassRoom(); // Gets a class with 5 students

        MockRepository fakeRepositoery = new MockRepository();
        IStudentReporter reporter = fakeRepositoery
                                    .StrictMock<IStudentReporter>();

        using(fakeRepositoery.Record())
           {
              reporter.PrintStudentReport(null);

              // We decalre constraint for parameter in 0th index 
              LastCall.Constraints(List.Count(Is.Equal(5))); 
           }

       reporter.PrintStudentReport(classRoom.Students);
       fakeRepositoery.Verify(reporter);
      }
pencilCake
  • 51,323
  • 85
  • 226
  • 363

1 Answers1

0

Ok. I found the way:

[Test]
public void PrintStudentReport_ClassRoomPassed_StudentListOfFive_WithAAA()
{
    //Arrange
    IClassRoom classRoom = GetClassRoom(); // Gets a class with 5 students
    IStudentReporter reporter = MockRepository.GenerateMock<IStudentReporter>();

    //Act
    reporter.PrintStudentReport(classRoom.Students);

    //Assert
    reporter
        .AssertWasCalled(r =>  r.PrintStudentReport(
                        Arg<List<IStudent>>
                               .List.Count(Is.Equal(5)))
                         );
}
pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • 3
    I think yould should use `GenerateStub` here. You only need `GenerateMock` if you want to use `.Expect`. See the user guidance about the [difference between GenerateMock and GenerateStub](http://www.ayende.com/wiki/Rhino+Mocks+3.5.ashx) (though to be honest the page as a whole is also a bit confusing... I'm referring to the part where it says "In most cases, you should prefer to use stubs. Only when you are testing complex interactions would I recommend to use mock objects.") – Wim Coenen Dec 09 '10 at 20:12