Pergunta

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?

Foi útil?

Solução

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");
    }

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top