我正在尝试模拟数据服务上下文,作为其中的一部分,我有一个接收的方法
当我尝试模拟这个方法时,MOQ总是返回一个
模拟上的所有调用都必须具有相应的设置。 TearDown:Moq.MockException:以下设置未匹配: IContext m => m.Retrieve(It.IsAny()})
接口/实现下面的代码
public interface IContext
{
IQueryable<T> Retrieve<T>(Expression<Func<T, bool>> predicate,
string entitySetName = null,
params Expression<Func<T, object>>[] eagerProperties);
}
public class Context : IContext
{
private readonly DataServiceContext _context;
public Context(DataServiceContext context)
{
this._context = context;
}
public IQueryable<T> Retrieve<T>(Expression<Func<T, bool>> predicate,
string entitySetName = null,
params Expression<Func<T, object>>[] eagerProperties)
{
DataServiceQuery<T> query = _context.CreateQuery<T>(entitySetName ?? "Default");
return eagerProperties.Aggregate(query, (current, e) => current.Expand(e.ToString())).Where(predicate);
}
}
以下是调用上述上下文方法的测试类
public class Test
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}
public class SomeController
{
private IContext _context = new Context(
new DataServiceContext(new Uri("http://whatever/someservice.svc/")));
public IContext ServiceContext
{
get
{
return _context ??
(_context = new Context(new DataServiceContext(new Uri("http://whatever/someservice.svc/"))));
}
set
{
_context = value;
}
}
public Test RetrieveSomeInformation()
{
IQueryable<Test> tests = _context.Retrieve<Test>
(
//Param 1
t => t.Prop1 == "test" && 1 == 1,
//Param 2
"Test",
//Param 3
t => t.Prop1,
t => t.Prop2,
t => t.Prop3
);
return tests.First();
}
}
以下是MOQ失败的实际测试,其中“模拟上的所有调用都必须具有相应的设置”。无法理解为什么地球上的设置不匹配!任何帮助将不胜感激。
[TestFixture]
public class ControllerTests
{
public MockRepository Repository { get; set; }
protected Mock<IContext> MockContext { get; set; }
public SomeController Controller;
public List<Test> Tests;
public Test Test;
[SetUp]
public void SetUp()
{
Test = new Test { Prop1 = "1", Prop2 = "2", Prop3 = "3" };
Tests = new List<Test> { Test };
Repository = new MockRepository(MockBehavior.Strict);
MockContext = Repository.Create<IContext>();
Controller = new SomeController { ServiceContext = MockContext.Object };
}
[TearDown]
public void TearDown()
{
Repository.VerifyAll();
}
[Test]
public void DetailProgramme_Test()
{
MockContext.Setup(m => m.Retrieve<Test>
(
//Param 1
It.IsAny<Expression<Func<Test, bool>>>(),
//Param 2
It.IsAny<string>(),
//Param 3
It.IsAny<Expression<Func<Test, object>>>()
)
).Returns(Tests.AsQueryable());
Test info = Controller.RetrieveSomeInformation();
//myMock.Setup(r => r.Find(It.IsAny<Expression<Func<Person, bool>>>())).Returns(new List<Person>() { new Person() }.AsQueryable());
Assert.IsTrue(info == Test);
}
}
答案 0 :(得分:5)
我相信你的设置就是这个......
//Param 3
It.IsAny<Expression<Func<Test, object>>>()
哪个与params数组不匹配。尝试...
//Param 3
It.IsAny<Expression<Func<Test, object>>[]>()
答案 1 :(得分:1)
在从中导出实例后,您正在设置模拟。获得对象后,对模拟的任何更改都不会影响。例如:
object instance = mock.Object; // this version wont include what you have configured in the setup
mock.Setup(...);
object instance2 = mock.Object; // get the latest version including whatever you have configured in the setup
修复代码的一种方法是从Setup方法中删除instantiate语句并使Controller变得懒惰,如:
public SomeController Controller = new Lazy<SomeController>(() => new SomeController() { ServiceContext = MockContext.Object });
这样,只要您在首次安装后使用控制器,您将始终使用最新版本。
答案 2 :(得分:1)
在没有It.IsAny<>
的情况下使用Moq的.CallBack
会强制您编写测试未涵盖的代码。相反,它允许任何查询/表达式通过,从单元测试的角度来看,使得模拟基本没用。
解决方案:你需要使用Callback来测试表达式,或者你需要更好地约束你的模拟。无论哪种方式都是混乱和困难的。只要我练习TDD,我就已经解决了这个问题。我终于把一个帮助类放在一起,使它更具表现力和更少杂乱。这是一个可能的最终结果:
mockPeopleRepository
.Setup(x => x.Find(ThatHas.AnExpressionFor<Person>()
.ThatMatches(correctPerson)
.And().ThatDoesNotMatch(deletedPerson)
.Build()))
.Returns(_expectedListOfPeople);
这是博客文章,讨论它并提供源代码:http://awkwardcoder.com/2013/04/24/constraining-mocks-with-expression-arguments/