jjzjj

ruby - 如何使用 rspec/rspec-mocks stub 类方法

我正在使用rspec-mock进行测试驱动开发。我开始实现单个类并使用rspec-mock模拟/stub其他类。尚未实现的类的模拟对象效果很好。但是,当我尝试模拟一个尚不存在的类的类方法时,我没有成功。我的类“哈希”应该有一个类方法“calculate_hashes”接收文件名并返回哈希。我试过了allow(Hashes).toreceive(:calculate_hash)do|file|#looksupwhattoreturnend给出错误“Hashesisnotaclass”。然后我实现了一个类“Hashes”classHashesend然后只尝试以相同的方式对类方法进行stub

ruby-on-rails - rspec 测试 has_many :through and after_save

我有一个(我认为)相对简单的has_many:through与连接表的关系:classUser:user_following_thing_relationshipsendclassThing:user_following_thing_relationships,:source=>:userendclassUserFollowingThingRelationship还有这些rspec测试(我知道这些不一定是好的测试,这些只是为了说明正在发生的事情):describeThingdobefore(:each)do@user=User.create!(:name=>"Fred")@thing=

ruby-on-rails - "rails generate rspec:install"似乎失败了

运行:ruby1.9.3p0和Rails3.2.1尝试使用rspec但当我尝试将其安装到我的应用程序中时出现以下错误:/Users/Si/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configuration.rb:85:in`method_missing':undefinedmethod`generators'for#(NoMethodError)from/Users/Si/.rvm/gems/ruby-1.9.3-p0/gems/rspec-rails-2.0.0.beta.18/lib/rspec-r

ruby - 如何测试正在使用 RSpec 和 Mocha 调用的混合类方法?

我有一个模块:moduleMyModuledefdo_something#...endend由类使用如下:classMyCommandextendMyModuledefself.execute#...do_somethingendend如何验证MyCommand.execute调用了do_something?我已经尝试使用mocha进行部分模拟,但是当未调用do_something时它不会失败:it"callsdo_something"doMyCommand.stubs(:do_something)MyCommand.executeend 最佳答案

ruby-on-rails - expect() 在 rspec/cucumber 中做了什么?

在MichaelHartl的RailsTutorial中,许多示例使用expect()方法。这是cucumber步骤定义中的一个这样的例子:Then/^sheshouldseeherprofilepage$/doexpect(page).tohave_title(@user.name)end同样的例子可以写成同样的效果:Then/^sheshouldseeherprofilepage$/dopage.shouldhave_title(@user.name)end为什么要使用expect()?它增加了什么值(value)? 最佳答案

ruby - 检测由 RSpec、Ruby 运行的代码

我想知道我的代码是否在rspec下运行。这可能吗?原因是我正在加载一些错误记录器,这些记录器在测试期间会被故意错误(expect{x}.toraise_error)弄得乱七八糟。我查看了我的ENV变量,没有(明显的)测试环境变量的迹象。 最佳答案 在spec_helper.rb的开头添加:ENV['RACK_ENV']='test'现在您可以在代码中检查RACK_ENV是否经过测试。 关于ruby-检测由RSpec、Ruby运行的代码,我们在StackOverflow上找到一个类似的问题

ruby - rspec 显示负时间

我在ruby​​1.9.3p0上运行rails3.2.1和rspec2.8.1,在运行我的测试时它显示负时间值。这很烦人,因为我正在尝试优化我的测试。Running:spec/models/transaction_spec.rb................................................Finishedin-7603162.49414seconds我已经尝试将rspec更新到2.9.0,但这没有帮助。 最佳答案 你在使用timecopgem吗?确保在卡住后Timecop.return。或者你在某处

ruby - 在 RUBY 上的 PADRINO 框架上使用 RSPEC 进行测试的教程

我是Ruby新手,并被要求在我们的新项目中使用它。我们还被要求使用Padrino(Sinatra)作为后端/框架。我们被要求使用Rspec进行测试。我一直在寻找可以指导在Padrino上使用RspecforRuby的教程。我得到的主要是引用RoR。但是,我需要RubyonPadrino。请在任何入门/指南/引用/讨论等方面指导我。如有不妥之处请指正。可能是我没有针对我的问题搜索正确的词/短语组合。我正在使用Ruby1.9.3和Padrinov.0.10.6。注意:我还提到了SOquestion,但它没有帮助。 最佳答案 我没用过Pa

ruby-on-rails - Rails Rspec 测试 Controller 新 Action

我正在尝试在我的Controller中测试新操作。目前它看起来像这样:Controllerdefnew@business=Business.new@business.addresses.buildend规范describe'GET#new'doit'assignsanewbusinessto@business'doget:newexpect(assigns(:business)).tobe_a_new(Business)endend我想测试“@business.addresses.build”这一行。我该怎么做?提前致谢! 最佳答案

ruby-on-rails - "undefined method ` stub_request '"访问 RSpec 支持文件中的方法时

我的Ruby-on-Rails项目中有以下文件结构,用于规范:/spec/msd/serviceservice_spec.rb/support/my_modulerequests_stubs.rb我的request_stubs.rb有:moduleMyModule::RequestsStubsmodule_functiondeflist_clientsurl="dummysite.com/clients"stub_request(:get,url).to_return(status:200,body:"clientsbody")endend在我的service_spec.rb我有:re