我已经创建了一个可以通过特殊url读取api数据的Controller。defcategory@data=get_api_data(param1)enddefget_api_data(param1)"http://my_api_url/param1=#{param1}"end创建View后,我可以从浏览器中看到结果。如果我使用rspec+capybara做特性测试visitcategory_path('param1')然后我想确认一个api数据将显示在View中expect(page).tohave_field('name',with:'aaa')但是@data值始终为空。为什么?是否
我正在为我未构建的应用程序编写Controller测试,因此这绝对是一个学习过程。这是我第一次遇到直接继承自AbstractController::Base的Controller。显然,它的行为与其他Controller不同。其格式大致为:classSchwadGenericController我尝试了正常测试,这是我目前要让任何事情发生的地方。require'rails_helper'describeSchwadGenericControllerdo#before(:each)do#SchwadGenericController.skip_authorize_resource#end
有没有推荐的测试方式Railsgenerators与RSpec?我找到的唯一解决方案是GeneratorSpecgem,已经两年多没有更新了。 最佳答案 有gemhttps://github.com/petergoldstein/generator_spec,虽然维护得不是很积极,但做得不错 关于ruby-on-rails-如何使用RSpec测试生成器,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/
我有33个规范以大约5秒的速度运行,以这种速度运行会导致测试套件变慢。我追踪到请求规范(4秒以上),因为模型规范只用了一小部分时间。我已经检查过,我的请求规范没有任何过于复杂或不必要的东西,所以我不知道该去哪里让它们更快,而不是只在推送代码之前运行它们以确保一切正常.加快请求规范的最佳方法是什么? 最佳答案 我使用Spork来加速我的测试。它保持整个环境加载以赢得时间。看看这个博客:http://ykyuen.wordpress.com/2010/12/14/rails-running-rspec-with-spork-test-s
我正在尝试使用RSpec为我在RoR中的测试创建自定义匹配器。define:be_accessibledo|attributes|attributes=attributes.is_a?(Array)?attributes:[attributes]attributes.eachdo|attribute|matchdo|response|response.class.accessible_attributes.include?(attribute)enddescription{"#{attribute}shouldbeaccessible"}failure_message_for_shou
我想优化一些较慢的规范。此类规范的示例如下所示: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
我在使用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
我希望我的后台作业能够内联运行某些标记测试。我可以通过用perform_enqueueddo包装测试来做到这一点,但我希望能够用元数据标记它们,如果可能的话,它会自动发生。我试过以下方法:it"doeseverythinginthejobtoo",perform_enqueued:truedoendconfig.around(:each)do|example|ifexample.metadata[:perform_enqueued]perform_enqueued_jobsdoexample.runendendend但它会导致错误:undefinedmethod`perform_enq
我不确定将几个模块包含到RSpec中的方式,所以让我描述一下我的情况。在app/helpers下,我有两个带有助手的文件,包含模块ApplicationHelper和MailersHelper。尽管这些是我在我的View和邮件中使用的View助手,但我也在我的测试中使用了它们的一些方法,因此它们必须可以在describe子句中访问。在app/spec/mailers下,我还有一个文件,包含模块Helpers。该模块包含仅在测试中使用的方法(主要是长期期望的包装方法)。此外,我还有以下代码:classHelpersincludeSingletonincludeActionView::He
我有一个看起来像这样的类: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上面的测试给出了一个关于