dongquexi1990 2014-10-14 21:50
浏览 23
已采纳

如何测试我的界面功能?

Third day in Go so pardon if this is a newb question ;). I am creating a simple calculator that will eventually have many different tasks: addition, subtraction, multiplication, etc, etc...Each of the tasks will have 2 functions: First and Second.

package main

import (
    "github.com/mytestproj/calculator"
)

type Calc interface {
    First(x int) int
    Second(x int) int
}

func main() {
    x := 16
    var i Calc
    a := calculator.Add{}
    i = a
    i.First(x)
    i.Second(x)
}

I currently have everything organized as:

github.com/
   mytestproj/
      calculator/
         addition.go
         addition_test.go
         subtraction.go
         subtraction_test.go
   main/
      main.go

In addition.go I have:

package calculator

type Add struct{}

func BasicAddition(x int) int {  // this won't be in the final release
    return x + 2
}

func (h Add) First(x int) int {
    x += 5
    return x
}

func (h Add) Second(x int) int {
    x += 10
    return x
}

And in addition_test.go I have:

package calculator

import "testing"

func TestBasicAddition(t *testing.T) {
    x := 30
    if y := BasicAddition(x); y != 32 {
        t.Errorf("Mine is %v", y)
    }
}

func TestFirst(t *testing.T) {
    x := 10
    if y := First(x); y != 15 {
        t.Errorf("First is %v", y)
    }
}

When I run my test I get an error:

# github.com/mytestproj/calculator
./addition_test.go:15: undefined: First
FAIL    github.com/mytestproj/calculator [build failed]

My question: How do I test "First?"

If I remove the test for First completely then the tests run fine and it passes.

Secondary question: The idea is to build a calculator app with many different functions. If there's a better way to organize the code then please let me know.

  • 写回答

1条回答 默认 最新

  • doudieyou5209 2014-10-14 23:37
    关注

    First is a method on type Add. For example,

    a := Add{}
    a.First(x)
    

    addition.go

    package calculator
    
    type Add struct{}
    
    func BasicAddition(x int) int { // this won't be in the final release
        return x + 2
    }
    
    func (h Add) First(x int) int {
        x += 5
        return x
    }
    
    func (h Add) Second(x int) int {
        x += 10
        return x
    }
    

    addition_test.go

    package calculator
    
    import "testing"
    
    func TestBasicAddition(t *testing.T) {
        x := 30
        if y := BasicAddition(x); y != 32 {
            t.Errorf("Mine is %v", y)
        }
    }
    
    func TestFirst(t *testing.T) {
        x := 10
        a := Add{}
        if y := a.First(x); y != 15 {
            t.Errorf("First is %v", y)
        }
    }
    

    Output:

    $ go test -v
    === RUN TestBasicAddition
    --- PASS: TestBasicAddition (0.00s)
    === RUN TestFirst
    --- PASS: TestFirst (0.00s)
    PASS
    ok      so/calculator   0.002s
    $
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!