저는 저장소 (EF가있는 데이터 액세스 레이어)를 호출하는 관리자 (비즈니스 계층)가 있습니다. 관리자의 논리는 두 개의 서로 다른 람다 식을 매개 변수로 사용하여 저장소의 메서드를 두 번 호출합니다.다른 λ 식으로 두 번 방법을 조롱 했습니까?
제 질문은 제 람다에 대해 주어진 응답을 반환하도록 제 저장소를 조롱하는 방법이지만 두 번째 람다에 대한 또 다른 응답을 반환합니까? 예를 들어
:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Relation
{
public int GiverId { get; set; }
public int ReceiverId { get; set; }
}
public interface IRelationRepository
{
bool Loves(Expression<Func<Relation, bool>> predicate);
}
public class RelationRepository : IRelationRepository
{
public bool Loves(Expression<Func<Relation, bool>> predicate)
{
// Some logic...
return true;
}
}
public class KissManager
{
private readonly IRelationRepository repository;
public KissManager(IRelationRepository repository)
{
this.repository = repository;
}
public bool Kiss(Person p1, Person p2)
{
var result = this.repository.Loves(r => r.GiverId == p1.Id && r.ReceiverId == p2.Id)
&& this.repository.Loves(r => r.GiverId == p2.Id && r.ReceiverId == p1.Id);
return result;
}
}
[TestMethod]
public void KissWithReceiverNotInLove()
{
// Arange.
var p1 = new Person { Id = 5, Name = "M. Love" };
var p2 = new Person { Id = 17, Name = "Paul Atreid" };
var kissRepositoryMock = new Mock<IRelationRepository>();
kissRepositoryMock
.Setup(m => m.Loves(r => r.GiverId == p1.Id && r.ReceiverId == p2.Id))
.Returns(true);
kissRepositoryMock
.Setup(m => m.Loves(r => r.GiverId == p2.Id && r.ReceiverId == p1.Id))
.Returns(false);
var kissManager = new KissManager(kissRepositoryMock.Object);
// Act.
var result = kissManager.Kiss(p1, p2);
// Assert.
Assert.IsFalse(result);
}
[SetupSequence (https://codecontracts.info/2011/07/28/moq-setupsequence-is-great-for-mocking/) 대신 Setup'의'. 올바른 순서로 설정했는지 확인하십시오. –
고마워, 작동 해! 그러나 관리자의 통화 순서를 변경하면 아무 기능도 변경되지 않지만 내 테스트는 실패합니다. 다른 방법을 알고 있습니까? – C0b0ll
SetupSequence의 순서는 SUT에있는 것과 일치해야합니다. SUT에서 통화 순서를 변경하면 테스트를 업데이트해야합니다. 테스트는이 점에서 취약합니다. –