如何使用的TestCase在NUnit的2.5? [英] How to use TestCase in NUnit 2.5?

查看:224
本文介绍了如何使用的TestCase在NUnit的2.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用NHibernate坚持到我的数据库货币类。 货币类看起来是这样的:

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



我已经写了使用 [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;
    }



我知道这是错误的,但我不知道怎么写。其结果可能是一个对象

推荐答案

属性参数(在结果)必须是一个常量表达式。像你现在要做的,你不能创建对象。

Attribute argument (for Result) must be a constant expression. You can't create objects like you do now.

使用测试用例属性是很好的,你需要测试的情况下验证多个简单的输入/输出。在你scenarion,但是,您可以做这样的事情(也就是,如果你只打算验证ID代码映射是否正确):

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):

[TestCase(6, Result = "GBP")]
[TestCase(7, Result = "USD")]
[TestCase(8, Result = "CAD")]
public string CanGetCurrencyById(int id)
{
    ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
    Currency c = currencies.GetById<Currency>(id);

    return c.Code;
}



此外,看一看的 测试用例文档 - 它们提供了相当不错的例子。

Also, take a look at TestCase documentation - they provide quite good examples.

修改
通过映射测试我的意思是验证你的ORM映射(NHibernate的数据库)是否正确,工作按预期。你通常测试在以下情形:

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:


  1. 创建与预设值(如新实体实例货币

  2. 开始新的事务

  3. 保存实体(保存 + 刷新 + 集中退出组合,以确保NHibernate的不保存的实体存储在缓存中了)

  4. <李>提取实体
  5. 使用预定义的人

  6. 回滚事务比较检索的值

  1. Create new entity instance with predefined values (eg. Currency)
  2. Start new transaction
  3. Save entity (Save + Flush + Evict combination to ensure NHibernate doesn't store saved entity in cache anymore)
  4. Retrieve entity
  5. Compare retrieved values with predefined ones
  6. Rollback transaction

如果这样的测试,然后通过,或多或少告诉你的我可以保存这个实体的值,然后我可以用完全相同值的检索它。而这一切你想知道 - 映射是正确的。

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.

使用寿测试用例属性,整个对象的验证正确性是相当困难的 - 它的意思是测试简单的东西。您可以使用变通像其他答案建议,但它很快变得无法读取,难以维护(想象与6+性质的实体验证)(通过测试用例传递参数)。

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 via TestCase) but it quickly becomes unreadable and hard to maintain (imagine entity with 6+ properties to verify).

我建议拆分测试成一个验证是否映射 ID 代码是正确的(但我看不出有什么点,这样做,除非你的总是的计划,以映射到特定的代码一定IDS)和另一种验证是否货币实体正确映射到数据库表。

I suggest splitting your test into one that verifies whether mapping of id to code 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 whether Currency entity is properly mapped to database table.

这篇关于如何使用的TestCase在NUnit的2.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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