【问题标题】:OneTimeSetUp: Invalid signature for SetUp or TearDown method: System-IDisposable-DisposeOneTimeSetUp:SetUp 或 TearDown 方法的签名无效:System-IDisposable-Dispose
【发布时间】:2018-11-07 14:30:06
【问题描述】:

我想将 nunit 的 teardownattribute 与 System.IDisposable 的实现结合使用,因为我想在 F# 中使用 use 关键字。为什么我在运行测试时会收到此错误?

[<TestFixture>] 
type public when_it_connects_to_database() =
    interface IDisposable with
        [<TearDown>]
        member this.Dispose() =
            this.connection.Dispose()

    member val public connection : ApplicationDbContext = createdatabasegateway true
        with get, set

    [<TestCase(true)>]
    member public this.it_succeeds(testmode:bool) : ApplicationDbContext =
        this.connection <- createdatabasegateway testmode
        this.connection

    [<Test>]
    member public this.it_can_read_the_database() =
        this.connection.AvailableExchanges.AsEnumerable().Count()

测试名称:it_can_read_the_database 测试 全名:tests.when_it_connects_to_database.it_can_read_the_database 测试 :第 29 行测试结果:失败测试持续时间:0:00:00.0000001

结果消息:OneTimeSetUp:SetUp 或 TearDown 的签名无效 方法:System-IDisposable-Dispose

【问题讨论】:

    标签: f# nunit nunit-3.0


    【解决方案1】:

    如果一个测试夹具实现了 IDisposable,NUnit 会在所有测试都运行并且任何标有OneTimeTearDownAttribute 的方法都运行后释放它。

    使用TearDownAttribute 标记您的 dispose 方法意味着您试图在 每个 测试之后处置该对象,并且显然对于第一次之后的任何测试都不健康。

    您看到的实际错误消息似乎隐藏了这样一个事实,即您根本不应该对这种方法使用TearDownAttribute。只需实现 IDisposable,对象就会在适当的时候被释放。

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多