草庐IT

unit-testing - 戈朗 : Replace function unit testing

coder 2023-07-01 原文

我正在使用 Golang,目前我正在用 Testify 做一些有趣的单元测试,我的文件看起来像这样

type myStruct struct {
  field_1 string

}
func (self *myStruct) writeFirst()  {
//doing something
//modify field_1
self.writeSecond()
}

func (self *myStruct) writeSecond() {
//doing something
}

在这种情况下,我正在测试 writeFirst(),但我正在尝试替换 writeSecond(),因为它使用了我不想使用的 http 内容,因为它可以访问互联网。

我认为使用第二个结构并将 myStruct 设置为匿名字段将是解决方案,但它不起作用,因为我的第二个结构和 myStruct 具有不同的上下文。

在这种情况下,我不能使用模拟,因为 writeSecond 是结构的一个方法。

我的测试用例是这样的:

func TestWriteFirst(t *testing.T) {
   myStc := myStruct{}
   assert.Equal(t,"My response", myStc.field_1)
}

我想要的只是测试 writeFirst 而不传递给 writeSecond()

最佳答案

为了说明 Not-a-Golfer 提到的那种重构在 the comments ,您可以考虑仅在作为接口(interface)的实例上调用第二个函数:

type F2er interface {
    Func2()
}

type S struct{ _f2 F2er }

var s = &S{}

func (s *S) f2() F2er {
    if s._f2 == nil {
        return s
    }
    return s._f2
}

func (s *S) Func1() {
    fmt.Println("s.Func1")
    s.f2().Func2()
}

此处:Func1s.f2() 上调用 Func2,而不是直接在 s 上调用。

  • 如果 s 中没有任何设置,s.f2() 返回...本身:s
  • 如果 s._f2 被任何其他实现 Func2struct 替换,s.f2()返回该实例而不是它本身。

请参阅此 playground script 中的完整示例.

输出:

TestFunc1
s.Func1
s.Func2

TestFunc1bis
s.Func1
testS.Func2    <=== different Func2 call

关于unit-testing - 戈朗 : Replace function unit testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791040/

有关unit-testing - 戈朗 : Replace function unit testing的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby 单元测试 : run some code after each failed test - 2

    在Test::Unit中的ruby​​单元测试断言失败后,在执行teardown之前,是否有一些简洁优雅的方法来立即执行我的代码?我正在做一些自动化的GUI测试,并希望在出现问题后立即截图。 最佳答案 如果您使用的是1.9,请不要使用Test::Unit::TestCase作为您的基类。对其进行子类化并覆盖#run_test以进行救援,截取屏幕截图并重新提出:classMyAbstractTestCase或者,我认为这实际上是最简洁的方法,您可以使用before_teardownHook:classMyTestCase这不适用于1.

  3. ruby-on-rails - 如何扩展 Ruby Test::Unit 断言以包含 assert_false? - 2

    显然在Test::Unit中没有assert_false。您将如何通过扩展断言并添加文件config/initializers/assertions_helper.rb来添加它?这是最好的方法吗?我不想修改test/unit/assertions.rb。顺便说一句,我不认为这是多余的。我使用的是assert_equalfalse,something_to_evaluate。这种方法的问题是很容易意外使用assertfalse,something_to_evaluate。这将始终失败,不会引发错误或警告,并且会在测试中引入错误。 最佳答案

  4. ruby-on-rails - 脚本/服务器 custom_require.rb :36:in `require' : cannot load such file -- test/unit/error (LoadError) - 2

    我有一个基于1.8.7构建的应用程序,我正尝试在1.9.3的系统上启动它当我运行脚本/服务器时,我得到:/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require':cannotloadsuchfile--test/unit/error(LoadError)from/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require'我的服务器脚本如下所示:#!/usr/bin/envrubyrequireFile.expand_path('../.

  5. ruby-on-rails - rspec rails 测试 : how can I force ActiveJob job's to run inline for certain tests? - 2

    我希望我的后台作业能够内联运行某些标记测试。我可以通过用perform_enqueueddo包装测试来做到这一点,但我希望能够用元数据标记它们,如果可能的话,它会自动发生。我试过以下方法:it"doeseverythinginthejobtoo",perform_enqueued:truedoendconfig.around(:each)do|example|ifexample.metadata[:perform_enqueued]perform_enqueued_jobsdoexample.runendendend但它会导致错误:undefinedmethod`perform_enq

  6. ruby-on-rails - Rails 5 升级 :/actionpack-5. 0.0/lib/action_controller/test_case.rb:49:in `initialize':参数数量错误(0 代表 2)(ArgumentError) - 2

    我最近正在进行Rails5升级,当我尝试启动Rails控制台时遇到了这个错误:/actionpack-5.0.0/lib/action_controller/test_case.rb:49:ininitialize':wrongnumberofarguments(0for2)(ArgumentError)当前bundleupdaterails已经完成了gem依赖项的解决,足以更新到5.0.0,rspec正在运行(尽管我正在修复很多中断)。我也可以运行railss没有错误。这里是代码中断行:https://github.com/rails/rails/blob/master/action

  7. ruby-on-rails - ruby rails : Best way to test a failed call to a third party API - 2

    我现在调用第三方网络服务作为我的应用程序的一部分。我正在使用RestClientgem来执行此操作。有大量工具可以用来做同样的事情,所以这应该无关紧要。我很好奇的是有足够好的测试,没有什么太花哨的,我可以在其中模拟当第三方Web服务出于任何原因不可用时我的应用程序如何响应。无论是我超出了速率限制还是由于网络延迟/并发症而超时,我只想能够获取HTTP状态代码之类的东西并测试我的应用程序在该事件中执行的操作。使用Test::Unit执行此操作的最佳方法是什么?现在,对第三方服务的调用封装在我的一个Controller中。我有一个简单的模块,其中包含一些用于远程服务不同端点的包装器方法。我只

  8. ruby - 我如何在不使用 ruby​​ 中的 sleep() 的情况下 rspec/test 一个 updated_at 字段? - 2

    如何在不使用sleep(1.second)方法的情况下编写规范?当我取消sleep时,我的测试会因为返回相同的时间戳而中断吗?我有以下类方法:defskipqs=find_or_create_by(user_id:user_id)qs.set_updated_atqs.n_skip+=1qs.save!end和以下规范:qs=skip(user.id)sleep(1.second)qs2=skip(user.id)qs.should_notbe_nilqs2.should_notbe_nil(qs.updated_at 最佳答案 我

  9. ruby-on-rails - `require' : No such file to load -- test_helper (LoadError) - 2

    当我在生产模式下运行我的Rails应用程序时出现以下错误,但是当我在开发模式下运行我的应用程序时它工作正常。我可以在生产模式下使用任何Gem吗?`require':Nosuchfiletoload--test_helper(LoadError)以下是完整的代码轨迹:/home/nyros/.rvm/gems/ruby-2.2.0@dfl/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in`require':Nosuchfiletoload--test_helper(LoadError)from/home/

  10. ruby-on-rails - 带有 Devise 和 rspec : Warden test helpers unreliable 的 Rails 3 - 2

    我有一个使用Rails3.2和Devise的应用程序。我有一个使用rspec和Capybara的请求测试套件。我尝试在我的登录助手中改用Warden测试助手,而不是让Capybara填写登录表并提交。由于我的测试套件的大小和复杂性,这使我的测试运行时间节省了超过一分半钟。在我的配置中是:RSpec.configuredo|config|config.includeWarden::Test::Helpers,:type=>:requestconfig.after:eachdoWarden.test_reset!endend在上下文中:let!(:current_user){Factory

随机推荐