如何用Nunit进行测试 [英] How to do test with Nunit
本文介绍了如何用Nunit进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个方法:
Hi , I have this method :
public static IEnumerable<W_H59MpxSessionFrais> ListeSessionFrais(MpxFraisContext db, int siteID, int stval, string codeFrais)
{
var sessionfrais = (from D1 in db.H59_MpxSessionFrais
where D1.CodeFrais == codeFrais
&& D1.SiteId == siteID
&& D1.StatusValidation == stval
select new W_H59MpxSessionFrais
{
ID = D1.SessionFraisId,
LibelleSession = D1.LibelleSession,
DateDebutSession = D1.DateDebutSession,
DateFinSession = D1.DateFinSession
});
return sessionfrais.ToList();
}
我想和Nunit一起测试,如果有人有想法请告诉我怎么做测试课知道我正在使用asp.net MVC 4,Razor和linq到实体
and i want to do test with Nunit ,if someone have an idea please tell me how to do the test class knowing that i'm working with asp.net MVC 4 ,Razor and linq to entities
推荐答案
你到底在哪里?
可能与MpxFraisContext db
参数。在这里你需要一个模拟。也就是说,该类型的对象包含一些数据或没有(取决于单数测试)。您可以自己创建这样的对象,或使用模拟框架,例如Moq。
函数可能如下所示:
Where exactly are you stuck?
Possibly with theMpxFraisContext db
parameter. Here you need a "mock". That is, an object of that type which contains some data or none (depending on the singular test). You may create such an object yourself, or use a "mocking framework", e.g. Moq.
A function could look like:
[Test]
public void TestListeSessionFrais()
{
MpxFraisContext db = [create and fill that object with useful data]
int siteID = 1;
int stval = 2;
string codeFrais = "a";
IEnumerable<w_h59mpxsessionfrais> retVal = [YourClass].ListeSessionFrais(db, siteID, stval, codeFrais);
int expectedCount = 1;
Assert.IsNotNull(retVal, "the function must return a value different from null");
//... some more Asserts on the returned object, e.g. count, properties of first object in list,...
}</w_h59mpxsessionfrais>
如何在测试类中使用mock?
how to use mock in test class ?
这篇关于如何用Nunit进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文