如何为以下linq查询创建单元测试 [英] How to create unit test for the following linq query

查看:61
本文介绍了如何为以下linq查询创建单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为以下linq查询创建单元测试

How to create unit test for the following linq query

public int CalculateRating(string hotelID)
     {
         var RatingQuery = from a in db.tbl_Reviews
                           where a.HotelID == hotelID
                           group a by a.HotelID into g
                           select new
                           {
                               TotalRating = g.Sum(a => a.Rating)
                           };
         int totalRating = Convert.ToInt16(RatingQuery.FirstOrDefault().TotalRating);

        int totalnoofRating = db.tbl_Reviews.Count(n => n.HotelID == hotelID);

        int newrating = (totalRating / totalnoofRating);

        return (newrating);
     }

推荐答案

您好,



任何一个方法可以针对某些预先计算的分析或基于某种可能性进行测试。



在这里你可以手动计算一家酒店的评级,你的测试用例会调用这个函数,你可以断言预期的结果(你已经知道)和实际从函数返回值。



在这里你可以测试CalculateRating函数作为一个单位。你不能只测试Linq查询,因为它是在私有变量中存储信息。



虽然您可以使用私有对象在单元测试中访问私有变量。



希望这些信息可以帮到你,



祝你好运。
Hi,

Any of the method can be tested against some pre-calculated analysis or based on some possibility.

Here you can have manually calculated rating for one hotel and your test case will call this function and you can Assert the expected result(that you already know) and the actual return value from the function.

Here you can test CalculateRating function as a unit. you can not test only Linq query as it is stores information in private variable.

Although you can access private variable in unit test using private object.

hope this information helps you,

Best luck.


这篇关于如何为以下linq查询创建单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆