System.NotSupportedException:不支持的表达式:p => (p.UserProfileId == 1) [英] System.NotSupportedException: Unsupported expression: p => (p.UserProfileId == 1)
问题描述
我有一个使用System.Func表达式的测试.应该很简单,但是测试仍然失败.
I have a test that uses System.Func expressions. It should be pretty straight forward, but the test keeps on failing.
测试:
[TestMethod]
public void GetUser()
{
var username = "john@resilientplc.com";
var user = new User() { UserId = 1, Username = username, Password = "123456789" };
someDataMock.Setup(s => s.GetUser(p => p.UserId == 1)).Returns(user);
var result = userProfileModel.GetUser(user.UserId);
Assert.AreEqual(user, result);
}
实施 UserProfileModel :
public User GetUser(long userId)
{
return someDataMock.GetUser(u => u.UserId == UserId);
}
错误:
System.NotSupportedException:不支持的表达式:p => (p.UserId == 1)
System.NotSupportedException: Unsupported expression: p => (p.UserId == 1)
任何知道我的测试不正确的地方吗?
Any idea where my test is incorrect?
推荐答案
假设您使用的是Moq,并且someDataMock是模拟对象,则问题出在安装程序上.试试这个吧...
Assuming that you're using Moq and that someDataMock is a mocked object, the problem is with the setup. Try this instead...
someDataMock.Setup(s => s.GetUser(It.IsAny<Func<User, bool>>()).Returns(userProfile);
这应该可以,但是您可能希望根据测试的性质对模拟接受的回调进行更多限制.
That should work, but you might want to make the mock a little bit more restrictive in what callbacks it accepts, depending on the nature of your test.
这篇关于System.NotSupportedException:不支持的表达式:p => (p.UserProfileId == 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!