Rhino 模拟断言.IsNotNull 失败
我遇到了 Rhino Mock 问题。我尝试了一切,但我的测试总是失败。 奇怪的是,上周四我单独运行了单元测试,结果成功了。然后我一起运行所有单元测试,有几个单元测试失败了。 (什么鬼??) 其中之一如下:
public interface IOrder
{
Allocation GetAllocation();
Group GetParentGroup();
}
public class Order : IOrder
{
public virtual int Id { get; set; }
public virtual GroupMembership ParentGroupMembership { get; set; }
public virtual Allocation GetAllocation()
{
var parentGroup = GetParentGroup();
return parentGroup.GetAllocationForOrder( this );
}
public virtual Group GetParentGroup()
{
return ParentGroupMembership.ParentGroup;
}
}
public interface IGroupMembership
{
}
public class GroupMembership : IGroupMembership
{
public virtual int Id { get; set; }
public virtual Group ParentGroup { get; set; }
public virtual Order Order { get; set; }
}
public interface IGroup
{
Allocation GetAllocationForOrder( Order order );
}
public class Group : IGroup
{
public virtual int Id { get; set; }
public virtual ICollection<Allocation> Allocations { get; private set; }
public Group()
{
Allocations = new List<Allocation>();
}
public virtual Allocation GetAllocationForOrder( Order order )
{
return Allocations.Single( a => a.OrderId == order.Id );
}
}
public interface IAllocation
{
}
public class Allocation : IAllocation
{
public virtual int Id { get; set; }
public virtual int OrderId { get; set; }
}
[TestMethod]
public void TestGetAllocation()
{
var order = MockRepository.Stub<Order>();
order.Id = 1;
var parentGroupMembership = MockRepository.Stub<GroupMembership>();
parentGroupMembership.Id = 2;
var parentGroup = MockRepository.Stub<Group>();
parentGroup.Id = 3;
parentGroupMembership.ParentGroup = parentGroup;
order.ParentGroupMembership = parentGroupMembership;
var allocation = new Allocation { Id = 4, OrderId = 1 };
using ( MockRepository.Record() )
{
Expect.Call( order.GetParentGroup() ).Return( parentGroup );
Expect.Call( parentGroup.GetAllocationForOrder( order ) ).Return( allocation );
}
using ( MockRepository.Playback() )
{
var actual = order.GetAllocation();
Assert.IsNotNull( actual );
Assert.AreEqual( actual.OrderId, allocation.OrderId );
}
}
问题是 IsNotNull 断言总是失败。为什么?在录音部分我将其设置为与孩子一起返回。我不明白。也许发生这种情况是因为 Children 集合,但在这种情况下我怎么能嘲笑该集合呢?
非常感谢, 大师
I'm stucked with a Rhino Mock issue. I tried everything but my test always fails.
The strange thing is that on last thursday I run the unit test alone and it worked. Then I run all the unit tests together and several were failed. (wtf??)
One of them is the following:
public interface IOrder
{
Allocation GetAllocation();
Group GetParentGroup();
}
public class Order : IOrder
{
public virtual int Id { get; set; }
public virtual GroupMembership ParentGroupMembership { get; set; }
public virtual Allocation GetAllocation()
{
var parentGroup = GetParentGroup();
return parentGroup.GetAllocationForOrder( this );
}
public virtual Group GetParentGroup()
{
return ParentGroupMembership.ParentGroup;
}
}
public interface IGroupMembership
{
}
public class GroupMembership : IGroupMembership
{
public virtual int Id { get; set; }
public virtual Group ParentGroup { get; set; }
public virtual Order Order { get; set; }
}
public interface IGroup
{
Allocation GetAllocationForOrder( Order order );
}
public class Group : IGroup
{
public virtual int Id { get; set; }
public virtual ICollection<Allocation> Allocations { get; private set; }
public Group()
{
Allocations = new List<Allocation>();
}
public virtual Allocation GetAllocationForOrder( Order order )
{
return Allocations.Single( a => a.OrderId == order.Id );
}
}
public interface IAllocation
{
}
public class Allocation : IAllocation
{
public virtual int Id { get; set; }
public virtual int OrderId { get; set; }
}
[TestMethod]
public void TestGetAllocation()
{
var order = MockRepository.Stub<Order>();
order.Id = 1;
var parentGroupMembership = MockRepository.Stub<GroupMembership>();
parentGroupMembership.Id = 2;
var parentGroup = MockRepository.Stub<Group>();
parentGroup.Id = 3;
parentGroupMembership.ParentGroup = parentGroup;
order.ParentGroupMembership = parentGroupMembership;
var allocation = new Allocation { Id = 4, OrderId = 1 };
using ( MockRepository.Record() )
{
Expect.Call( order.GetParentGroup() ).Return( parentGroup );
Expect.Call( parentGroup.GetAllocationForOrder( order ) ).Return( allocation );
}
using ( MockRepository.Playback() )
{
var actual = order.GetAllocation();
Assert.IsNotNull( actual );
Assert.AreEqual( actual.OrderId, allocation.OrderId );
}
}
The problem is that the IsNotNull assert always fails. Why? In the recording section I set it to return with the child. I don't understand it. Maybe this happens because the Children collection, but in this case how could I mock that collection?
Many thanks,
Maestro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能使用 Rhino Mocks 存根虚拟方法。因此,请确保您的 GetChild 方法是虚拟的,否则您的
Expect.Call(parent.GetChild( null ) ).Return( child );
对非虚拟方法没有任何意义。您无法定义对非虚拟成员的期望。因此,请尝试将该方法声明为虚拟方法:
此外,您在示例中使用的 Rhino Mocks 中的记录/播放语法已被弃用,取而代之的是 AAA 语法(实际上这个弃用发生在 2008 年,当时 AAA 语法被引入)。
而且你的单元测试也很难理解。您在这里到底要测试什么?您尝试进行单元测试的方法是您正在定义期望的方法 -
GetChild
。这很奇怪,也不是嘲笑的目的。您仅模拟被测方法所依赖的依赖项。您定义对这些依赖项的期望,以指导被测方法的流程并断言不同的场景。更新:
:而不是
您应该使用
:来实例化被测试的主题。
如果您使用
Stub
方法,则您尝试在此处进行单元测试的实际方法 (order.GetAllocation
) 将永远不会被命中。You can only stub virtual methods with Rhino Mocks. So make sure that your GetChild metyhod is virtual, otherwide your
Expect.Call( parent.GetChild( null ) ).Return( child );
makes no sense on a non-virtual method. You cannot define expectations on non-virtual members.So try declaring the method as virtual:
Also the Record/Playback syntax in Rhino Mocks which you are using in your example has been deprecated in favor of the AAA syntax (actually this deprecation happened back in 2008 when the AAA syntax was introduced).
Also your unit test is difficult to understand. What exactly are you testing here? The method you are trying to unit test is the method you are defining expectations on -
GetChild
. That's weird and not how mocking is intended to be used. You mock only dependencies that the method under test relies upon. You define expectations on those dependencies in order to guide the flow of the method under test and assert different scenarios.UPDATE:
Instead of:
you should use:
in order to instantiate the subject under test.
If you use the
Stub
method, the actual method that you are trying to unit test here (order.GetAllocation
) will never be hit.