我有一个假的存储库,我需要根据表达式返回不同的值
A.CallTo(() => repository.FirstOrDefaultAsync<Group>(x => x.Id == busMessage.Id && x.GroupType == ObjectType.Office)).Returns(dbOffice);
A.CallTo(() => repository.FirstOrDefaultAsync<Group>(x => x.Id == busMessage.Id && x.GroupType == ObjectType.Country)).Returns(dbCountry);
我尝试了不同的方法,但是没有运气 任何建议表示赞赏
答案 0 :(得分:1)
尝试单独定义您的表情。它对我有用。
using System;
using NUnit.Framework;
using FakeItEasy;
using System.Linq.Expressions;
namespace ClassLibraryTests
{
[TestFixture]
public class IRepositoryTests
{
private IRepository CreateSut(Expression<Func<Group, bool>> expression, object result)
{
IRepository sut = A.Fake<IRepository>();
A.CallTo(() => sut.FirstOrDefaultAsync<Group>(expression)).Returns(result);
return sut;
}
[Test]
public void FirstOrDefaultAsyncTests_Office()
{
// Arrange
BusMessage busMessage = new BusMessage();
Expression<Func<Group, bool>> expression = x => x.Id == busMessage.Id && x.GroupType == ObjectType.Office;
object dbOffice = new object();
IRepository repository = this.CreateSut(expression, dbOffice);
// Act
object obj = repository.FirstOrDefaultAsync<Group>(expression);
//Assert
Assert.AreEqual(obj, dbOffice);
}
[Test]
public void FirstOrDefaultAsyncTests_Country()
{
// Arrange
BusMessage busMessage = new BusMessage();
Expression<Func<Group, bool>> expression = x => x.Id == busMessage.Id && x.GroupType == ObjectType.Country;
object dbCountry = new object();
IRepository repository = this.CreateSut(expression, dbCountry);
// Act
object obj = repository.FirstOrDefaultAsync<Group>(expression);
//Assert
Assert.AreEqual(obj, dbCountry);
}
}
public interface IRepository
{
object FirstOrDefaultAsync<T>(Expression<Func<T, bool>> expression);
}
public class Group
{
public ObjectType GroupType { get; set; }
public int Id { get; set; }
}
public class BusMessage
{
public int Id { get; set; }
}
public enum ObjectType
{
Office,
Country
}
}