Rhino 모의 문제가 있습니다. 나는 모든 것을 시도했지만 내 시험은 항상 실패합니다. 이상한 점은 지난 주 목요일에 내가 단원 테스트를 단독으로 실행한다는 것입니다. 그럼 모든 단위 테스트를 함께 실행하고 여러 실패했습니다.Rhino mock assert.IsNotNull failed
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 컬렉션 때문일 수도 있지만이 경우 어떻게 컬렉션을 모의 할 수 있을까요?
많은 감사, 마에스트로
죄송합니다, 내 예제가 잘못되었습니다. GetChild 메서드의 결과를 반환하는 메서드를 테스트하려고합니다. – maestro
@maestro,이 경우 코드를 표시 할 수 있습니까? 지금 당장 볼 수있는 것은 GetChild 메서드에 대한 기대치를 정의하고 있고이 메서드를 호출하는 2 줄 뒤에'var actualChild = parent.GetChild (null);'을 추가 한 다음 그 결과에 대해 어떤 것을 주장한다는 것입니다. –
예제를 편집하여 실제 문제와 정확히 동일하게 만듭니다. 어쨌든, 그 경우에 actualChild를 썼습니다. null이 아니어야합니다. 맞습니까? – maestro