如何在 NUnit 2.5 中使用 TestCase?

发布于 2024-12-27 05:52:35 字数 745 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2025-01-03 05:52:35

属性参数(对于 Result)必须是常量表达式。您无法像现在一样创建对象。

使用 TestCase 属性非常适合测试需要验证多个简单输入/输出的案例。然而,在您的场景中,您可以执行类似的操作(也就是说,如果您只打算验证 id 代码映射是否正确):

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

另外,请查看 TestCase 文档 - 他们提供了非常好的示例。

编辑
通过映射测试,我的意思是验证您的 ORM 映射(NHibernate 到数据库)是否正确并按您的预期工作。您通常在以下场景中进行测试:

  1. 使用预定义值(例如 Currency)创建新实体实例
  2. 启动新事务
  3. 保存实体(Save + Flush + Evict 组合以确保 NHibernate 不再将保存的实体存储在缓存中)
  4. 检索实体
  5. 将检索到的值与预定义的值进行比较
  6. 回滚事务

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

不过,使用 TestCase 属性,验证整个对象的正确性相当困难 - 它的目的是测试简单的东西。您可以使用其他答案中建议的解决方法(通过 TestCase 传递参数),但它很快就会变得不可读且难以维护(想象一下具有 6 个以上属性的实体需要验证)。

我建议将您的测试分成一个测试,以验证 idcode 的映射是否正确(但是我认为这样做没有什么意义,除非您总是 计划将某些 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):

[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.

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. 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.

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

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.

落花浅忆 2025-01-03 05:52:35

在这种情况下,我将预期结果的构造函数参数传递到测试用例中,然后自己进行检查。虽然它不是那么简洁,但它完成了工作。

[TestCase(6, "GBP", "British Pound", "£")]
public void CanGetCurrencyById(int id, string code, string name, string symbol)
{
    ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
    Currency c = currencies.GetById<Currency>(id);
    Assert.That(c, Is.EqualTo(new Currency(code, name, symbol)));
}

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.

[TestCase(6, "GBP", "British Pound", "£")]
public void CanGetCurrencyById(int id, string code, string name, string symbol)
{
    ICurrencyRepo currencies = new RepoFactory().CreateCurrencyRepo(_session);
    Currency c = currencies.GetById<Currency>(id);
    Assert.That(c, Is.EqualTo(new Currency(code, name, symbol)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文