render_views 返回 NoMethodError [英] render_views returning NoMethodError

查看:43
本文介绍了render_views 返回 NoMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读 Michael Hartl 的 Ruby on Rails 教程,在教程的第 3.2.2 节中,他谈到了使用 RSpec 来测试页面标题.我的测试(我自己写的,按照他的教程)一直没有通过标题测试(它可以找到标签,但标题总是一个空字符串),在谷歌上的一些搜索告诉我我需要包含函数 render_views 以通过测试.我的问题是,无论我尝试什么,RSpec 都会为 render_views 返回一个 NoMethodError.我尝试过的事情:

I'm currently going through the Ruby on Rails Tutorial by Michael Hartl, and in Section 3.2.2 of the tutorial, he talks about using RSpec to test for the titles of the page. My tests (which I wrote myself, following his tutorial) kept failing the title tests (it can find the tag, but the title is always a blank string), and some searching on Google told me I needed to include the function render_views to pass the tests. My problem is that no matter what I try, RSpec returns a NoMethodError for render_views. The things I've tried:

  • config.render_viewsspec_helper.rb
  • 描述静态页面"做渲染视图...在规范文件中结束
  • 描述静态页面"做RSpec::Rails::ViewRendering.render_views...在规范文件中结束

它们都返回一个 NoMethodError.

我没有完全遵循他的 Gemfile,所以 Ruby 和相关 gem 的版本是:

I'm not following his Gemfile exactly, so the versions of Ruby and the relevant gems are:

  • Ruby:1.9.3-p125
  • 导轨:3.2.9
  • rspec:2.12.0
  • rspec-rails:2.12.0
  • 水豚:2.0.1

我在 Windows 7 上使用 RubyMine 4.5.4.但是,在命令提示符下运行测试会返回相同的错误.规范文件是 static_pages_spec.rb 并且位于 spec/requests

I'm working in RubyMine 4.5.4, on Windows 7. Running the test in a command prompt returns the same errors however. The spec file is static_pages_spec.rb and is located in spec/requests

我的static_pages_spec.rb中的代码:

require_relative '../spec_helper'

describe "Static Pages" do
  #RSpec::Rails::ViewRendering.render_views, returns NoMethodError
  #render_views, returns NameError

  describe "Home Page" do
    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1', :text => "Sample App")
    end

   it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "Help Page" do
    it "should have the h1 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('h1', :text => "Help")
    end

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About Page" do
    it "should have the h1 'About Us'" do
      visit '/static_pages/about'
      page.should have_selector('h1', :text => "About Us")
    end

    it "should have the title 'About Us'" do
      visit '/static_pages/about'
      page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us")
    end
  end
end

我的spec_helper.rbspec_helper.rb,是在用创建项目后通过运行rails generate integration_test自动生成的——-no-test-framework,在 config 块中添加这些行:

My spec_helper.rb is the spec_helper.rb automatically generated by creating running rails generate integration_test after creating the project with --no-test-framework, with these lines added in the config block:

# Fix NoMethodError for method 'visit'
config.include Capybara::DSL

config.include Capybara::RSpecMatchers

编辑经过一些测试,我设法通过将水豚版本更改为 1.1.2 来使测试通过,同时仍然使用 rspec 2.12.0 和 rspec-rails 2.12.0 而使用 render_views.虽然我很高兴测试通过了,但它仍然没有解决缺少 render_views 方法的问题,如果我尝试使用它仍然会出现.

EDIT After some testing, I've managed to get the tests to pass by changing the capybara version to 1.1.2, while still using rspec 2.12.0 and rspec-rails 2.12.0 and not using render_views. While I'm glad the tests are passing, it still doesn't solve the problem of the missing render_views method, which still crops up if I try to use it.

推荐答案

好吧,经过多一点测试,缺少 render_views 方法的谜团解决了.要包含渲染视图方法,在调用 config.render_views 之前,显然需要 spec_helper 中的 config.include RSpec::Rails::ViewRendering规范文件中的 spec_helperrender_views.

Well, after a bit more testing, the mystery of the missing render_views method is solved. To include the render views method, apparently config.include RSpec::Rails::ViewRendering in the spec_helper is needed before calling config.render_views in the spec_helper or render_views in the spec file.

其次,关于测试通过或失败,问题似乎出在 Capybara 本身.显然,visit 方法在 1.1.2 和 2.0.1 中的实现方式似乎在返回的内容上有很大的不同;即使在包含 render_views 之后,测试仍然失败(不使用 config.include Capybara::RSpecMatchers).不过,使用它似乎可以深入了解其实现;Capybara 使用 RSpecMatchers 的失败消息是它正在寻找 CSS 而不是 HTML.但是,我并没有声称完全理解它失败的原因,所以如果有人能启发我解释为什么 Capybara 2.0.1 导致测试失败而 1.1.2 允许测试通过,那就太好了.

Secondly, with regards to the tests passing or failing, the problem seems to be in Capybara itself. Apparently, it seems that the way the visit method is implemented in 1.1.2 and 2.0.1 is significantly different in what it returns; even after including render_views, the test still fails (without using config.include Capybara::RSpecMatchers). Using it seems to provide an insight into its implementation though; the failure message from Capybara on using the RSpecMatchers is that it is looking for CSS instead of HTML. I don't claim to fully understand why it fails, however, so if anyone can enlighten me as to why Capybara 2.0.1 causes the tests to fail while 1.1.2 allows the tests to pass, that would be great.

这篇关于render_views 返回 NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆