此测试使用NUnit 2.5和Rhino.Mocks 3.5:
[TestFixture]
public class ExcelWorksheetColumnTests
{
private const string _name = "F1";
private const int _index = 0;
private MockRepository _repo;
[SetUp]
public void SetUp(){_repo = new MockRepository();}
private ExcelWorksheetColumn CreateColumnUsingFixtureFieldsButWith(IExcelWorksheet excelWorksheet)
{
return new ExcelWorksheetColumn(_name, _index, excelWorksheet);
}
[Test]
public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
{
var stubWorksheet = _repo.Stub<IExcelWorksheet>();
ExcelWorksheetColumn column =
CreateColumnUsingFixtureFieldsButWith(stubWorksheet);
stubWorksheet
.Stub(p => p.GetSelectedShootColumnType(column))
.Return(ShootColumnType.Generic);
_repo.ReplayAll();
bool itHappened = false;
column.SelectedShootColumnTypeChanged
+= (s, e) => { itHappened = true; };
column.SelectedShootColumnType = ShootColumnType.SubjectId;
Assert.That(itHappened);
}
[TearDown]
public void TearDown()
{
_repo.ReplayAll();
_repo.VerifyAll();
}
}
收到此消息失败:
Rhino.Mocks.Exceptions.ExpectationViolationException: IExcelWorksheet.SetSelectedShootColumnType( ClassPhotolink.Model.CreateSubjectList.ExcelWorksheetColumn,SubjectId);
预期#0,实际#1。
我认为Stubs用于返回值或忽略方法调用。为什么要尝试验证对
的调用IExcelWorksheet.SetSelectedShootColumnType(
IExcelWorksheetColumn column,
ShootColumnType type)
在犀牛模拟wiki中,它表示关于Stub扩展方法“请记住,默认情况下 Rhino Mocks将忽略意外的方法调用。”
测试编辑。
删除SetUp&amp; TearDown代码,这仍然失败:
[Test]
public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
{
var repo = new MockRepository();
var stubWorksheet = repo.Stub<IExcelWorksheet>();
ExcelWorksheetColumn column = CreateColumnUsingFixtureFieldsButWith(stubWorksheet);
stubWorksheet
.Stub(p => p.GetSelectedShootColumnType(column))
.Return(ShootColumnType.Generic);
repo.ReplayAll();
bool itHappened = false;
column.SelectedShootColumnTypeChanged += (s, e) => { itHappened = true; };
column.SelectedShootColumnType = ShootColumnType.SubjectId;
Assert.That(itHappened);
}
但这正如我所希望的那样。
[Test]
public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
{
var stubWorksheet = MockRepository.GenerateStub<IExcelWorksheet>();
ExcelWorksheetColumn column = CreateColumnUsingFixtureFieldsButWith(stubWorksheet);
stubWorksheet
.Stub(p => p.GetSelectedShootColumnType(column))
.Return(ShootColumnType.Generic);
bool itHappened = false;
column.SelectedShootColumnTypeChanged += (s, e) => { itHappened = true; };
column.SelectedShootColumnType = ShootColumnType.SubjectId;
Assert.That(itHappened);
}
所以它似乎与repo.Stub()和MockRepository.GenerateStub();
有关。答案 0 :(得分:3)
使用Stub()
表示您执行期望进行方法调用。 意外调用是对未设置期望的方法的调用。
我现在想知道问题是你使用的是设置,拆卸和记录/重放,这是不必要的。
如果以这种方式编写测试会发生什么:
[TestFixture]
public class ExcelWorksheetColumnTests
{
private const string _name = "F1";
private const int _index = 0;
private ExcelWorksheetColumn CreateColumnUsingFixtureFieldsButWith(IExcelWorksheet excelWorksheet)
{
return new ExcelWorksheetColumn(_name, _index, excelWorksheet);
}
[Test]
public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
{
var stubWorksheet = MockRepository.Stub<IExcelWorksheet>();
ExcelWorksheetColumn column =
CreateColumnUsingFixtureFieldsButWith(stubWorksheet);
stubWorksheet
.Stub(p => p.GetSelectedShootColumnType(column))
.Return(ShootColumnType.Generic);
bool itHappened = false;
column.SelectedShootColumnTypeChanged
+= (s, e) => { itHappened = true; };
column.SelectedShootColumnType = ShootColumnType.SubjectId;
Assert.That(itHappened);
}
}