添加wicked_pdf gem会导致rspec视图规范因NoMethodError而失败

时间:2017-02-07 00:02:30

标签: ruby-on-rails rspec wicked-pdf

我有一个奇怪的情况,我不知道如何开始调试它。会喜欢任何人可以提供的建议或指示。

我有64个rspec测试 - 视图,模型,控制器,功能 - 它们都通过了。只要我将gem 'wicked_pdf'添加到Gemfile,执行bundle install,然后重新运行测试,所有16个视图规范都会失败,但所有其他测试运行正常。不仅如此,整个应用程序运行良好,本地和Heroku。因此,只有rspec才有问题;也许wicked_pdf中的某些内容与rspec相冲突......?

我不认为这个问题本身就存在于我的代码中,但我会添加一些我能想到的关于它包含线索的机会。 16个现在失败的观点规范之一是:

describe "properties/show.html.erb" do

   it "displays a field's name and value" do
     assign(:property,
       FactoryGirl.build_stubbed(:property, stories: 2))

     render
     expect(rendered).to match(/Stories.*2/m)
   end
   ... rest ommitted

其中一条错误消息是(它们都是一样的):

 1) properties/show.html.erb displays a field's name and value
     Failure/Error: <%= ps_markup_field_or_hide("Year Built", @property.yearbuilt, false,

     ActionView::Template::Error:
       undefined method `ps_markup_field_or_hide' for #<#<Class:0x007f2b1123e0e0>:0x0000000968d978>
     # ./app/views/properties/_show_body_post.html.erb:6:in `_app_views_properties__show_body_post_html_erb__2822263843794330255_69911472833980'
     # ./app/views/properties/show.html.erb:19:in `_app_views_properties_show_html_erb__3990796008770089996_79086380'
     # ./spec/views/properties/show.html.erb_spec.rb:19:in `block (2 levels) in <top (required)>'
     # /usr/local/rvm/gems/ruby-2.3.0/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `ps_markup_field_or_hide' for #<#<Class:0x007f2b1123e0e0>:0x0000000968d978>
     #   ./app/views/properties/_show_body_post.html.erb:6:in `_app_views_properties__show_body_post_html_erb__2822263843794330255_69911472833980'

包含红鲱鱼&#34;未定义方法的视图&#34;包含许多这样的调用到ps_markup_field_or_hide辅助函数:

<%= ps_markup_field_or_hide("Year Built", @property.yearbuilt, false,
                            @property.yearbuiltflag, "A",
                            " (actual)", " (estimated)") %>

该功能在app / helpers / views / properties / show_helper.rb中定义:

module Views::Properties::ShowHelper
  def ps_markup_field_or_hide(name, value, hide=false, 
                              ext_field=nil, ext_conf_val=nil,
                              ext_true_str=nil, ext_false_str=nil
                              )
    title = "<div class='hanging-indent'> " +
       "<span class='ps-details-titles'> #{name}: </span>"

    conf_str = ""
    if (ext_conf_val != nil)
      conf_str = ext_false_str
      if (ext_field != nil && ext_field.upcase == ext_conf_val.upcase)
          conf_str = ext_true_str
      end
    end

    if (value != nil)
      ("    " + title + value + conf_str + "\n      </div>").html_safe
    else
      ("    " + title + "Not on File\n      </div>").html_safe unless hide
    end
  end
end #module Views::Properties::ShowHelper

我开始黑客攻击,试图解决一个变通方法,所以我盲目地(我是一个红宝石初学者)向控制器添加了include Views::Properties::ShowHelper语句;然后我尝试在视图中对方法的完全限定路径进行硬编码:Views::Properties::ShowHelper.ps_markup_field_or_hide。我甚至将ps_markup_field_or_hide()方法分配给控制器中的视图变量,并尝试从视图中调用该方法。没有什么工作 - 每次都有同样的错误 - 而且我的尝试变得如此笨拙,即使我变得厌恶并退缩。

我的Gemfile:

source 'https://rubygems.org'

gem 'rails',        '5.0.0.1'
gem 'responders',   '2.3.0'
gem 'puma',         '3.4.0'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.4.1'
gem 'redcarpet',    '~> 3.0.0'
gem 'normalize-scss', '~> 4.0', '>= 4.0.3'
gem 'activemodel-serializers-xml'
gem 'draper', github: 'drapergem/draper'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
gem 'fuzzy-string-match'

group :development, :test do
  gem 'byebug',  '9.0.0', platform: :mri
  gem 'spring-commands-rspec'
  gem 'rspec-rails', '~> 3.5'
  gem 'guard-rspec'
  gem 'capybara', '~> 2.5'
  gem 'selenium-webdriver'
  gem 'factory_girl_rails', '~> 4.5.0'
  gem 'database_cleaner', '1.3.0'
end

group :development, :test, :production do
  gem 'mysql2', '>= 0.3.18', '< 0.5'
end

group :development do
  gem 'web-console',           '3.1.1'
  gem 'listen',                '3.0.8'
  gem 'spring',                '1.7.2'
  gem 'spring-watcher-listen', '2.0.0'
  gem 'pry-byebug'
end

group :test do
  gem 'sqlite3'
  gem 'rails-controller-testing', '0.1.1'
  gem 'minitest-reporters',       '1.1.9'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
#  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我正在使用Cloud9 IDE进行开发。

还有什么可能有帮助...这里是config / initializers / wicked_pdf.rb:

# WickedPDF Global Configuration
# https://github.com/mileszs/wicked_pdf/blob/master/README.md
if Rails.env.production?
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-binary-0.12.3.1/bin/wkhtmltopdf"
  WickedPdf.config = { exe_path: wkhtmltopdf_path, wkhtmltopdf: wkhtmltopdf_path }
else
  WickedPdf.config = {
  }
end

我正在查看https://github.com/rspec/rspec-rails/issues/1644,其中添加了rails-controller-testing gem导致rspec因NoMethodError而失败,但我无法看到如何将该帖子上的任何建议应用于此情况。我尝试修改Gemfile,添加&#34; require:false&#34; (gem 'wicked_pdf', require: false),重新运行bundle install,并将require 'wicked_pdf'放在其他几个地方,但这只会导致其他错误,我觉得我在黑暗中徘徊,而不是在正确的方向。

总而言之,我对如何进行感到难过,如果有人有任何想法,或者可以建议下一个调试步骤,我真的很感激。

非常感谢你 - 德里克。

PS。我使用:xvfb-run bundle exec spring rspec spec --format doc在cloud9上运行我的测试,我正在使用外部mysql数据库。我也有能力在sqlite本地运行我的测试,我通常为速度做,因为外部服务是如此之慢,但我已经尝试使用mysql和sqlite测试,我得到相同的错误消息

PPS。我目前的解决方法是我注释掉wicked_pdf gem和wicked_pdf.rb文件,重新运行bundle install并运行所有测试,然后当我开始编写PDF规范时,我会转回去上&#34; wicked_pdf的东西,只是单独运行PDF规范。但这太乱了,而且我想让测试一下子全部运行干净,这样我就可以进行CI工作流程(在待办事项列表中)。

$ bundle show rails /usr/local/rvm/gems/ruby-2.3.0/gems/rails-5.0.0.1
$ bundle show wicked_pdf /usr/local/rvm/gems/ruby-2.3.0/gems/wicked_pdf-1.1.0
$ bundle show rspec-rails /usr/local/rvm/gems/ruby-2.3.0/gems/rspec-rails-3.5.2

0 个答案:

没有答案