如何在 Massive 中测试没有结果

发布于 2024-12-08 22:04:15 字数 676 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-12-15 22:04:15

我这样做是为了测试我的查询是否返回记录。

控制器:

 public ActionResult Index() {
        _logger.LogInfo("In home");
        //var data = _tricksTable.All(orderBy: "DateCreated");
        dynamic viewModel = new ExpandoObject();
        var data = _tricksTable.Query("SELECT TOP(10) * FROM Tricks ORDER BY DateCreated DESC");
        viewModel.TenTricksNewestFirst = data;

        var data2 = _tricksTable.Query("SELECT TOP(10) * FROM Tricks ORDER BY Votes DESC");
        viewModel.TenTricksMostPopularFirst = data2;
        return View(viewModel);
    }

和测试:

[Test]
    public void a_user_should_be_able_to_view_10_newest_tricks_in_latest_videos_tab() {
        var result = _controller.Index() as ViewResult;
        dynamic viewModelExpando = result.ViewData.Model;
        var queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;

        var i = Enumerable.Count(queryFromMassiveDynamic);
        Assert.AreNotEqual(0, i, "TenTricksNewestFirst returned 0 records");
        Assert.LessOrEqual(i,10, "Ten Tricks Newest First returned more than 10 tricks");
    }

I'm doing this to test whether my query comes back with records.

Controller:

 public ActionResult Index() {
        _logger.LogInfo("In home");
        //var data = _tricksTable.All(orderBy: "DateCreated");
        dynamic viewModel = new ExpandoObject();
        var data = _tricksTable.Query("SELECT TOP(10) * FROM Tricks ORDER BY DateCreated DESC");
        viewModel.TenTricksNewestFirst = data;

        var data2 = _tricksTable.Query("SELECT TOP(10) * FROM Tricks ORDER BY Votes DESC");
        viewModel.TenTricksMostPopularFirst = data2;
        return View(viewModel);
    }

and test:

[Test]
    public void a_user_should_be_able_to_view_10_newest_tricks_in_latest_videos_tab() {
        var result = _controller.Index() as ViewResult;
        dynamic viewModelExpando = result.ViewData.Model;
        var queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;

        var i = Enumerable.Count(queryFromMassiveDynamic);
        Assert.AreNotEqual(0, i, "TenTricksNewestFirst returned 0 records");
        Assert.LessOrEqual(i,10, "Ten Tricks Newest First returned more than 10 tricks");
    }
审判长 2024-12-15 22:04:15

供以后寻求答案的人参考,这样做实际上更简单。不要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文