如何使用Rhino Mocks框架在AAA语法中编写此简单测试?
问题描述:
如何在带有Rhino Mocks框架的AAA语法中编写此简单的基于记录和重放的测试?如何使用Rhino Mocks框架在AAA语法中编写此简单测试?
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);
}
答
好的。我找到了方法:
[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)))
);
}
我想yould应该在这里使用`GenerateStub`。如果你想使用`.Expect`,你只需要'GenerateMock`。查看关于[GenerateMock和GenerateStub之间的区别](http://www.ayende.com/wiki/Rhino+Mocks+3.5.ashx)的用户指南(尽管老实说整个页面也有点混乱。 ..我指的是它所说的部分:“在大多数情况下,你应该更喜欢使用存根,只有当你测试复杂的相互作用时,我才会推荐使用模拟对象。”) – 2010-12-09 20:12:24