RhinoMocks Expect 无法正常工作 'expected'
RhinoMocks Expect not working as 'expected'
我有以下测试代码
//setup the mock
SomeResource mock = MockRepository.GenerateMock<SomeResource>();
mock.Stub(m => m.GetNumOfResources()).Return(1);
mock.Stub(m => m.SomeProp).Return(SomeEnum.YO);
mock.Expect(m => m.SomeProp).Repeat.Once();
mock.Replay();
//execute
SomeClass sc = new SomeClass();
sc.SetSomeResource(mock);
sc.MethodToTest();
//verify
mock.VerifyAllExpectations();
我想验证是否访问了 SomeProp。每当我调试代码时,我都可以看到 SomeProp
正在被访问,但我在上面设置的期望在测试中抛出异常,说它不是。我是 Rhino Mocks 的新手,所以我显然没有正确设置某些东西,但我看不到什么。有什么想法吗?
编辑:这基本上是我正在测试的code/logic:
private bool MethodToTest()
{
bool ret= false;
if (resource == null)
{
try
{
resource = new SomeResource();
}
catch (Exception e)
{
//log some error
}
}
if (resource != null && resource.GetNumResources() > 0)
{
bool ok = true;
try
{
resource.SetSomething("blah");
}
catch (Exception)
{
ok = false;
// Test that SomeProp was accessed here
SomeEnum val = resource.SomeProp;
}
ret = ok;
}
return ret;
}
mock 和 stub 之间有点 API 混淆,因为它们与 Rhino Mocks API 和基于交互的测试有关
//Arrange
SomeObj mock = MockRepository.GenerateMock<SomeObj>();
mock.Expect(_ => _.GetNumOfThings()).Return(1);
mock.Expect(_ => _.SetSomething(Arg<string>.Any())).Throw(new Exception());
mock.Expect(_ => _.SomeProp).Return(SomeEnum.YO).Repeat.Once();
SomeClass sc = new SomeClass();
sc.SetSomeResource(mock);
//Act
sc.MethodToTest();
//Assert
mock.VerifyAllExpectations();
我有以下测试代码
//setup the mock
SomeResource mock = MockRepository.GenerateMock<SomeResource>();
mock.Stub(m => m.GetNumOfResources()).Return(1);
mock.Stub(m => m.SomeProp).Return(SomeEnum.YO);
mock.Expect(m => m.SomeProp).Repeat.Once();
mock.Replay();
//execute
SomeClass sc = new SomeClass();
sc.SetSomeResource(mock);
sc.MethodToTest();
//verify
mock.VerifyAllExpectations();
我想验证是否访问了 SomeProp。每当我调试代码时,我都可以看到 SomeProp
正在被访问,但我在上面设置的期望在测试中抛出异常,说它不是。我是 Rhino Mocks 的新手,所以我显然没有正确设置某些东西,但我看不到什么。有什么想法吗?
编辑:这基本上是我正在测试的code/logic:
private bool MethodToTest()
{
bool ret= false;
if (resource == null)
{
try
{
resource = new SomeResource();
}
catch (Exception e)
{
//log some error
}
}
if (resource != null && resource.GetNumResources() > 0)
{
bool ok = true;
try
{
resource.SetSomething("blah");
}
catch (Exception)
{
ok = false;
// Test that SomeProp was accessed here
SomeEnum val = resource.SomeProp;
}
ret = ok;
}
return ret;
}
mock 和 stub 之间有点 API 混淆,因为它们与 Rhino Mocks API 和基于交互的测试有关
//Arrange
SomeObj mock = MockRepository.GenerateMock<SomeObj>();
mock.Expect(_ => _.GetNumOfThings()).Return(1);
mock.Expect(_ => _.SetSomething(Arg<string>.Any())).Throw(new Exception());
mock.Expect(_ => _.SomeProp).Return(SomeEnum.YO).Repeat.Once();
SomeClass sc = new SomeClass();
sc.SetSomeResource(mock);
//Act
sc.MethodToTest();
//Assert
mock.VerifyAllExpectations();