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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试解决空手道砍 kata练习,并因我的测试用例而陷入了此编译器错误:

too 多次调用this.T.common.Fail

我将 testing.T 包装到带有其他方法的结构中,作为 匿名结构字段:

 包main 
 
import(
“ fmt” 
“测试” 
)
 
type断言struct {
 * testing。  T 
} 
func(此断言)assert_equal(期望的int,实际的int){
如果(期望的==实际){
 this.Fail(fmt.Sprintf(“断言%v为%v的失败”, 实际的,预期的)); 
} 
} 
 
func TestChop(t * testing.T){
 test:= assertions {t} 
 
 test.assert_equal(-1,Chop(3,[  ] int {}))
 test.assert_equal(-1,Chop(3,[] int {1}))
 ... 
} 
   
 
 <  p>我希望 this.Fail 可以在上调用 Fail() 匿名的 testing.T 结构字段,它带有字符串参数。 为什么不是这种情况, this.T.common.Fail 来自何处? 我在 testing 包文档中找不到对 common 的任何引用。 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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)
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题