草庐IT

ruby - 在 RSpec 中捕获和断言 Meta 标签

我正在尝试编写一个单元测试来断言几个元标记值(在响应部分下)。如何使用属性标签检查单个值?例如:到目前为止,这就是我所得到的。但它返回失败。it"shouldcheckmetafields"doget:show,{:format=>'html'}r=response.bodyr.shouldhave_selector('meta',:content=>@site.title)end 最佳答案 Capybara的have_selector匹配器采用一个选项:text,而不是:content:r.shouldhave_selector(

ruby-on-rails - 如何使用 RSpec 更新测试模型中的属性

我被RSpec中的一个规范所困。如何更新模型的属性以使其为零?在更新Controller规范下进行验证更好吗?下面是一个示例代码。describeUserdodescribe".validation"dobefore(:each)do@user=User.create!({:username=>"dexter_morgan"})end...context"giveninvalidattributes"do#howcanImaketheusernamenil?it"rejectsblankusername"endendend 最佳答案

ruby-on-rails - Rails Rspec 模型规范用户:电子邮件 ActiveRecord::RecordInvalid

试图找出我的rspec测试失败的原因。最值得注意的是看起来自相矛盾的失败消息。声明我有一个ActiveRecord::RecordInvalid错误,这正是我断言应该发生的事情。这是我的user.rb...validates_presence_of:email...这是我的users_spec.rb...it"isinvalidwithoutemail"doFactory(:user,email:nil).shouldraise_error(ActiveRecord::RecordInvalid)end...这是输出:Failures:1)Userauser(ingeneral)isi

ruby - 在 rspec 中匹配参数的属性值

有没有办法在rspec中匹配参数的属性value?检查thedocumentation看起来还有其他可用的匹配器,例如anything、an_instance_of和hash_including-但没有检查对象属性值的方法。示例-假设我有这个实例方法:classDateParserdefparse_year(a_date)putsa_date.yearendend然后我可以这样写一个期望:dp=DateParser.newexpect(dp).toreceive(:parse_year).with(some_matcher)我想让some_matcher检查parse_year是用任何

ruby-on-rails - rspec - 找不到命令

作为作业的一部分,我正在创建一个基本网站。当rails服务器未运行且我对根目录执行“rspec”时,将执行rspec。但是测试失败。当我启动railsserver并从另一个终端窗口执行rspec时(进入根目录后),rspec不起作用。我收到以下消息-Tushars-MacBook-Pro:recipefindertusharsaurabh$rspec-bash:rspec:commandnotfound请告诉我如何修复它。 最佳答案 也许你的rspec命令没有在系统范围内安装,没关系,试试运行bundleexecrspec它应该可以

ruby-on-rails - 使用 RSpec 测试用户身份验证

我使用简单的Rails4.1.0应用程序,不使用设计。我正在测试应用程序Controller:classApplicationController我对authenticate_user!方法有疑问。完整规范seehere.方法规范:describe'authenticate_user!'docontext'whenuserloggedin'dobefore{subject.send(:current_user=,create(:user))}it'donothing'doexpect(subject.send(:authenticate_user!)).toeqnilendendcon

ruby-on-rails - 确保 Rollbar 已使用 RSpec 报告错误

我的类中的逻辑有时使用Rollbar.silenced来忽略一些异常(因此它们不会被报告)。我正在尝试编写一个测试以确保rollbar实际报告错误。it'doesnotmuterollbar'doexpect(Rollbar).not_toreceive(:silenced)expect(Rollbar).toreceive(:error).with(any_args).and_call_originalexpect{query}.toraise_error(unknown_exception)end不幸的是,在报告未挽救的错误时,rollbar不会在:error、:critical、

ruby - 用 cucumber 评估多个 rspec 期望

我正在对cucumber使用rspec期望值。似乎在cucumber步骤中使用多个期望时,cucumber会评估它们直到第一个失败。但是,我想继续运行,其他人也希望清楚地了解出了什么问题。我能以某种方式实现这一点吗?例子:-让我们假设response="1",code="2"andstatus="3"expect(response).toeq("0")expect(code).toeq("2")expect(status).toeq("1")Cucumber在评估响应变量时会失败。但我想检查其他两个变量的值并获得错误状态值的输出。这可能吗? 最佳答案

ruby - 在 RSpec instance_double 上 stub 一个 setter

在RSpec单元测试中,我有一个像这样定义的模拟:let(:point){instance_double("Point",:to_coords=>[3,2])}在Point类中,我还有一个setter,用于被测类(称为Robot)。我想stub那个setter来测试Robot#move。这是我到目前为止的错误代码:describe"#move"doit"sets@xand@yonestepforwardinthedirectiontherobotisfacing"dopoint.stub(:coords=).and_return([4,2])robot.moveexpect(robot

ruby - 有没有办法强制 RSpec 重新评估 let 语句?

这个例子有点做作,但很好地解释了用例。let(:number_of_users){User.count}it'countsusers'doUser.createnumber_of_users.should==1User.createnumber_of_users.should==2end此测试失败,因为number_of_users仅评估一次,并且变得陈旧。有没有办法在每次调用时重新评估它? 最佳答案 您可以只定义一个常规方法:defnumber_of_usersUser.countendit'countsusers'doUser.