dtbiszu7724 2015-08-23 22:21
浏览 41
已采纳

使用testing.T作为匿名结构字段:“调用this.T.common.Fail的参数过多”

I am trying to solve the Karate Chop kata in Go as an exercise and stuck with this compiler error from my test case:

too many arguments in call to this.T.common.Fail

I wrapped testing.T into a struct with additional methods, as an anonymous struct field:

package main

import (
    "fmt"
    "testing"
)

type assertions struct {
    *testing.T
}
func (this assertions) assert_equal(expected int, actual int) {
    if (expected != actual) {
        this.Fail(fmt.Sprintf("Failed asserting that %v is %v", actual, expected));
    }
}

func TestChop(t *testing.T) {
  test := assertions{t}

  test.assert_equal(-1, Chop(3, []int{}))
  test.assert_equal(-1, Chop(3, []int{1}))
  ...
}

I expect this.Fail to call Fail() on the anonymous testing.T struct field, which takes a string parameter. Why isn't this the case and where does this.T.common.Fail come from? I cannot find any reference to common in the testing package documentation.

  • 写回答

1条回答 默认 最新

  • doucai1901 2015-08-23 22:34
    关注

    Source file src/testing/testing.go

    // Fail marks the function as having failed but continues execution.
    func (c *common) Fail() {
      c.mu.Lock()
      defer c.mu.Unlock()
      c.failed = true
    }
    
    // common holds the elements common between T and B and
    // captures common methods such as Errorf.
    type common struct {
      mu       sync.RWMutex // guards output and failed
      output   []byte       // Output generated by test or benchmark.
      failed   bool         // Test or benchmark has failed.
      skipped  bool         // Test of benchmark has been skipped.
      finished bool
    
      start    time.Time // Time test or benchmark started
      duration time.Duration
      self     interface{}      // To be sent on signal channel when done.
      signal   chan interface{} // Output for serial tests.
    }
    
    // T is a type passed to Test functions to manage test state and support formatted test logs.
    // Logs are accumulated during execution and dumped to standard error when done.
    type T struct {
      common
      name          string    // Name of test.
      startParallel chan bool // Parallel tests will wait on this.
    }
    

    func (*T) Fail

    func (c *T) Fail()
    

    Fail marks the function as having failed but continues execution.

    There are no arguments to T.common.Fail().

    Try Errorf:

    func (*T) Errorf

    func (c *T) Errorf(format string, args ...interface{})
    

    Errorf is equivalent to Logf followed by Fail.

    For example,

    this.Errorf("Failed asserting that %v is %v", actual, expected)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式