草庐IT

ruby-on-rails - RSpec 中单个设置的多个断言

我想优化一些较慢的规范。此类规范的示例如下所示:require'rspec'classHeavyComputationdefcompute_resultsleep1#somethingcomputeheavyhere"verybigstring"endenddescribeHeavyComputation,'preferredstyle,butslow'dosubject{described_class.new.compute_result}it{shouldinclude'big'}it{shouldmatch'string'}it{shouldmatch/very/}#+50oth

ruby-on-rails - Rspec 的 instance_double 创建间歇性规范失败

我在使用instance_double时遇到间歇性测试失败。我有一个包含4个规范的文件。这是来源:require'rails_helper'describeSubmitPostdobefore(:each)do@post=instance_double('Post')allow(@post).toreceive(:submitted_at=)endcontext'onsuccess'dobefore(:each)doallow(@post).toreceive(:save).and_return(true)@result=SubmitPost.call(post:@post)endit

ruby-on-rails - 如何访问设计 token 授权注册 Controller ?

我正在使用Deviseauthtokengem用于验证我的Rails应用程序的某些部分。但是,当我尝试使用注册路径创建新用户时,出现以下错误{"errors":["Authorizedusersonly."]}。这是我用于测试的rspec代码,it'createsauserusingemail/passwordcombo'dopostapi_user_registration_path,{email:'xxx',password:'yyy',password_confirmation:'yyy'}putslast_response.bodyexpect(last_response.bo

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

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

ruby-on-rails - 我可以在 :all with capybara? 之前使用吗

我有一个这样的描述block:describe"Documents"dosubject{page}let(:course){FactoryGirl.create(:course)}describe"new"dobeforedovisitnew_course_document_path(course)fill_in"Name",with:"TestDocument"attach_file"Originaldocument","#{Rails.root}/spec/fixtures/03_GUI_concurrency.pdf"endit{shouldhave_selector('titl

ruby-on-rails - 在 Rails/Capybara/Poltergeist 规范中使用 url_for 将驱动程序发送到 example.com 而不是应用程序

如果我在功能规范中调用url_for,它会返回一个以http://www.example.com/开头的绝对URL.Capybara会很乐意尝试加载该站点上的页面,但这与我的应用程序无关。以下是重现该问题的最少步骤:从这个Gemfile开始:source'https://rubygems.org'gem"sqlite3"gem"jquery-rails"gem"draper"gem"rails",'4.1.0'gem"therubyracer"gem"uglifier"gem"rspec-rails"gem"capybara"gem"poltergeist"gem"launchy"运行

ruby-on-rails - 如何在 RSpec 中包含多个模块?

我不确定将几个模块包含到RSpec中的方式,所以让我描述一下我的情况。在app/helpers下,我有两个带有助手的文件,包含模块ApplicationHelper和MailersHelper。尽管这些是我在我的View和邮件中使用的View助手,但我也在我的测试中使用了它们的一些方法,因此它们必须可以在describe子句中访问。在app/spec/mailers下,我还有一个文件,包含模块Helpers。该模块包含仅在测试中使用的方法(主要是长期期望的包装方法)。此外,我还有以下代码:classHelpersincludeSingletonincludeActionView::He

ruby-on-rails - 如何 stub 事件记录关系以使用 rspec 测试 where 子句?

我有一个看起来像这样的类:classFoo在测试#nasty_bars_present?我想编写一个rspec测试来对bars关联进行stub,但允许where自然执行。像这样的东西:describe"#nasty_bars_present?"docontext"withnastybars"dobefore{foo.stub(:bars).and_return([mock(Bar,bar_type:"Nasty")])}it"shouldreturntrue"doexpect(foo.nasty_bars_present?).tobe_trueendendend上面的测试给出了一个关于

ruby-on-rails - 如何完全重新加载 ActiveRecord 以重置其内存值?

在RSpec测试中,我创建了一个记录,其中包含多个内存值。foo.reload对对象的属性按预期工作,但内存的属性仍然存在。到目前为止,它通过完全重新创建对象来工作:foo=Foo.find(123)但在我的例子中,查找记录的逻辑实际上更复杂。什么是完全重新加载记录并删除所有内存值的好方法? 最佳答案 好的方法是您已有的方法:完全重新创建对象。您不能以任何简单的“Rails”方式“重新加载”对象的内存值,因为内存属性不是Rails或ActiveRecord的功能。两者都不知道您是如何内存方法的。

ruby-on-rails - 葡萄 : Rescue from invalid JSON

首先:我正在使用grape构建我的API(Rails4)。当有人发送无效的JSON正文时(例如忘记最后一个}),会引发以下错误:ActionDispatch::ParamsParser::ParseError(795:unexpectedtokenat'{"foobar":1234')我尝试使用grapesrescue_from:all选项,但这不起作用。在堆栈跟踪中,我没有看到涉及的葡萄gem。这个错误似乎是从actionpack中抛出的:.gems/gems/actionpack-4.1.4/lib/action_dispatch/middleware/params_parser.