如何在 Massive 中测试没有结果
我在 Massive 类上创建了 Find 方法。问题是我刚刚意识到它总是返回 true,因为结果永远不为空。这是我的代码:
public dynamic Find(string email)
{
dynamic result = new ExpandoObject();
dynamic experience = new ExpandoObject();
result.Success = false;
experience = this.Query(@"SELECT we.* FROM WorkExperience we
LEFT JOIN Members m ON m.Id = we.MemberId
WHERE m.Email = @0", email);
if (experience != null)
{
result.Experience = experience;
result.Success = true;
}
return result;
}
在这种情况下,经验永远不会为空,因此 result.Success 总是返回 false。如何测试 this.Query() 是否返回一条或多条记录?
I've created a Find method on a Massive class. Problem is I've just realised it always returns true as the results are never null. Here's my code:
public dynamic Find(string email)
{
dynamic result = new ExpandoObject();
dynamic experience = new ExpandoObject();
result.Success = false;
experience = this.Query(@"SELECT we.* FROM WorkExperience we
LEFT JOIN Members m ON m.Id = we.MemberId
WHERE m.Email = @0", email);
if (experience != null)
{
result.Experience = experience;
result.Success = true;
}
return result;
}
In this case experience is never null so result.Success always comes back as false. How can I test whether this.Query() returns a record or records?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这样做是为了测试我的查询是否返回记录。
控制器:
和测试:
I'm doing this to test whether my query comes back with records.
Controller:
and test:
供以后寻求答案的人参考,这样做实际上更简单。不要使用 DynamicModel 的 Query 方法,而是调用 Scalar 方法并确保您选择 a) 仅一个值,然后测试结果是否为 null 或 b) 匹配项计数,您可以从中将结果转换为整数并根据返回的数字进行测试。
For reference of future answer-seekers, this is actually much more simply done. Instead of using the Query method of the DynamicModel, call the Scalar method and make sure you either select a) just one value and then test if result is null or b) the count of matches, from which you can convert the result to an integer and test based on the number returned.