草庐IT

ruby-on-rails - 如何在 Rails 中使用 Rspec stub 错误引发?

我是Rails和Rspec的新手,我正在使用Rspec来测试这个包含异常处理的Controller方法:defsearch_movies_director@current_movie=Movie.find(params[:id])begin@movies=Movie.find_movies_director(params[:id])rescueMovie::NoDirectorErrorflash[:warning]="#{@current_movie}hasnodirectorinfo"redirect_tomovies_pathendend我不知道如何正确测试上述路径:在无效搜索后

ruby-on-rails - RSpec - 日期应该在两个日期之间

如何测试日期以查看它是否介于两个日期之间?我知道我可以做两个大于和小于比较,但我想要一个RSpec方法来检查日期的“betweeness”。例如:it"isbetweenthetimerange"doexpect(Date.now).tobe_between(Date.yesterday,Date.tomorrow)end我试过expect(range).tocover(subject)但没有成功。 最佳答案 Date.today.shouldbe_between(Date.today-1.day,Date.today+1.day)

ruby - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec

我有以下测试:let(:client){Descat::Client.new}describe'poblacio'doit'shouldsetformatcorrectly'doclient.poblacio('v1','json','dades')expect(client.instance_variable_get(:format)).toeq('json')endend我有以下正在测试的代码:moduleDescatclassClientBASE_URL='http://api.idescat.cat/'definitialize(attributes={})attributes

ruby - "it"关键字在 RSpec 中有什么作用?

我正在学习rails3tutorial,但在进行如下测试时,我不明白“it”关键字的含义:require'spec_helper'describeUsersControllerdorender_viewsdescribe"GET'new'"doit"shouldbesuccessful"doget'new'response.shouldbe_successendit"shouldhavetherighttitle"doget'new'response.shouldhave_selector("title",:content=>"Signup")endendend代码片段来自:http:

ruby-on-rails - RSpec 找不到我的 Controller 未初始化的常量

我的Rails应用程序最近从Rails3迁移到Rails4,我一直在尝试在个别规范(Controller、模型等)上运行rspec,但它似乎在实际定位对象时遇到了问题。例如,当我尝试运行以下代码时,它会出现未初始化常量错误。这似乎发生在多个Controller上。我已经尝试删除rails_helper.rb和spec_helper.rb并运行railsgeneraterspec:install但它似乎没有解决错误。为什么找不到Controller?我要执行的操作->rspecspec/controllers/activity_controller_spec.rb/Users/osx_u

ruby-on-rails - RSpec 的期望如何在 ROR 中工作

在阅读MichaelHartl的RubyOnRails教程时,在作者编写集成测试以验证他的注册页面的部分中,他使用了下面的代码spinet。我明白了代码的作用,但无法理解“如何”部分,即无法理解执行顺序。expect{click_button"Createmyaccount"}.not_tochange(User,:count)有人可以解释一下上述方法链和block的语义以及它们是如何组合在一起的吗? 最佳答案 您将使用expect...change来验证特定方法调用是否更改或不更改其他值。在这种情况下:expect{click_b

ruby-on-rails - 名称错误 : uninitialized constant Factory

我正在关注thistutorial为了与工厂女孩、rspec一起开始使用TDDonrails,我遇到了这个问题,我无法理解。这是我的“工厂”.rb(events.rb)require'faker'FactoryGirl.definedofactory:eventdoname"HIGH"genre"house,techno,idb"venue_name"WestbourneStudios"venue_address"4-6ChamberlayneRoad"venue_postcode"NW103JD"begin_time"10pm"end_time"2am"user_id2descrip

ruby 摩卡 : is there an equivalent to rspec-mocks' #and_call_original?

Rspec-mocks具有expect(some_object).toreceive(:some_method).and_call_original。我可以用Mocha做这个吗?如果可以,怎么做?some_object.expects(:some_method).......什么? 最佳答案 我相当确定没有办法做到这一点。浏览sourcecode,有评论提到完全替换了原来的方法。#Theoriginalimplementationofthemethodisreplacedduringthetestandthenrestoredatt

ruby - 如何使用 rspec 在 ruby​​ 中模拟 super?

我通过创建一个扩展到库类的子类来扩展现有的库。在子类中,我能够测试initialize方法中的大部分功能,但无法模拟super调用。子类如下所示。classChild如何编写rspec测试(使用mocha)以便我可以模拟super调用?请注意,我正在测试Child类中的initialize方法的功能。当提供额外参数时,我是否必须创建不调用super的单独代码路径? 最佳答案 你不能模拟super,你也不应该。当您模拟某些内容时,您正在验证是否收到了特定消息,而super不是一条消息——它是一个关键字。相反,弄清楚如果缺少super调

ruby - 使用 RSpec 测试输出到命令行

我想做的是在命令行上运行rubysayhello.rb,然后接收HellofromRspec。我已经明白了:classHellodefspeakputs'HellofromRSpec'endendhi=Hello.new#bringsmyobjectintoexistencehi.speak现在我想在rspec中编写一个测试来检查命令行输出实际上是“HellofromRSpec”而不是“我喜欢Unix”不工作。我目前在我的sayhello_spec.rb文件中有这个require_relative'sayhello.rb'#pointstofilesoIcan'see'itdescri