如何在 NUnit 2.5 中使用 TestCase?
我有一个 Currency
类,我使用 NHibernate 将其保存到我的数据库中。 Currency
类看起来像这样:
public class Currency : Entity
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual string Symbol { get; set; }
}
我已经使用 [TestCase]
编写了一个单元测试,如下所示:
[TestCase(6,Result = new Currency ({ Code="GBP", Name="British Pound", Symbol="£"}))]
public Currency CanGetCurrencyById(int id)
{
ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
Currency c = currencies.GetById<Currency>(id);
return c;
}
我知道这是错误的,但我不确定如何编写它。结果可以是一个对象
吗?
I have a Currency
class which I persist to my database using NHibernate. Currency
class looks like this:
public class Currency : Entity
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual string Symbol { get; set; }
}
I have written a unit test using [TestCase]
like this:
[TestCase(6,Result = new Currency ({ Code="GBP", Name="British Pound", Symbol="£"}))]
public Currency CanGetCurrencyById(int id)
{
ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
Currency c = currencies.GetById<Currency>(id);
return c;
}
I know this is wrong but I'm not sure how to write it. Can the result be an object
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性参数(对于
Result
)必须是常量表达式。您无法像现在一样创建对象。使用
TestCase
属性非常适合测试需要验证多个简单输入/输出的案例。然而,在您的场景中,您可以执行类似的操作(也就是说,如果您只打算验证 id 代码映射是否正确):另外,请查看
TestCase
文档 - 他们提供了非常好的示例。编辑:
通过映射测试,我的意思是验证您的 ORM 映射(NHibernate 到数据库)是否正确并按您的预期工作。您通常在以下场景中进行测试:
Currency
)创建新实体实例Save
+Flush
+Evict
组合以确保 NHibernate 不再将保存的实体存储在缓存中)如果这样的测试通过,它或多或少会告诉您 I可以用这些值保存这个实体,然后我可以使用完全相同的值检索它。。这就是您想知道的一切 - 映射是正确的。
不过,使用 TestCase 属性,验证整个对象的正确性相当困难 - 它的目的是测试简单的东西。您可以使用其他答案中建议的解决方法(通过 TestCase 传递参数),但它很快就会变得不可读且难以维护(想象一下具有 6 个以上属性的实体需要验证)。
我建议将您的测试分成一个测试,以验证
id
到code
的映射是否正确(但是我认为这样做没有什么意义,除非您总是 计划将某些 ID 映射到某些代码),另一个验证Currency
实体是否正确映射到数据库表。Attribute argument (for
Result
) must be a constant expression. You can't create objects like you do now.Using
TestCase
attribute is good for testing cases where you need to verify multiple simple inputs/outputs. In your scenarion, you can however do something like this (that is, if you only plan to verify whether id-code mapping is correct):Also, take a look at
TestCase
documentation - they provide quite good examples.Edit:
By mapping testing I meant verifying whether your ORM mappings (NHibernate to database) are correct and work as you intended. You usually test that in following scenario:
Currency
)Save
+Flush
+Evict
combination to ensure NHibernate doesn't store saved entity in cache anymore)If such test then passes, it more or less tells you that I can save this entity with those values, and I can then retrieved it with the exactly same values. And that's all you wanted to know - mappings are correct.
With
TestCase
attribute tho, verifying correctness of entire objects is quite difficult - it's meant to test simple stuff. You can use workarounds like suggested in other answer (passing arguments viaTestCase
) but it quickly becomes unreadable and hard to maintain (imagine entity with 6+ properties to verify).I suggest splitting your test into one that verifies whether mapping of
id
tocode
is correct (however I see little point in doing that, unless you always plan to have certain ids mapped to certain codes) and other one verifying whetherCurrency
entity is properly mapped to database table.在这种情况下,我将预期结果的构造函数参数传递到测试用例中,然后自己进行检查。虽然它不是那么简洁,但它完成了工作。
In cases like this I pass constructor arguments for the expected result into the test case, and, do the check myself. Although it is not as concise, it gets the job done.