rspec - 如何在渲染 View 上测试文本?

标签 rspec ruby-on-rails-3.2 rspec2

这是我的测试:

require "rspec"

describe HomeController do
  render_views

  it "should renders the home" do
    get :home
    response.should render_template("home")
    response.should include_text("Simulate Circuits Online")
  end

end

但是,我得到了:
1) HomeController should renders the home
     Failure/Error: response.should include_text("Some text to be test ..")
     NoMethodError:
       undefined method `include_text' for #<RSpec::Core::ExampleGroup::Nested_1:0xd429a0c>
     # ./spec/controllers/home_controller_spec.rb:9:in `block (2 levels) in <top (required)>'
     # (eval):6:in `block in fork'
     # (eval):6:in `fork'
     # (eval):6:in `fork'

那么,测试呈现的文本的正确方法是什么?

编辑
如果我尝试 response.should have_content("My text ..")
我得到
1) HomeController should renders the home
     Failure/Error: response.should have_content("My text ..")
       expected there to be text "My text .." in "#"
     # ./spec/controllers/home_controller_spec.rb:9:in `block (2 levels) in <top (required)>'

最佳答案

在您的 View 模板中,您确实有要检查的文本吗?错误似乎是它实际上无法找到文本。

另外,尝试使用 response.should have_text .

关于rspec - 如何在渲染 View 上测试文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14516516/

相关文章:

ruby-on-rails-3 - Rspec cookies.permanent,未定义方法

ruby-on-rails - Rails,OmniAuth,google_oauth2,google-api-client,Moments.insert ... 401未经授权...为什么?

ruby-on-rails - 对象ID和youtube视频ID与youtube_it gem相同

ruby-on-rails - 如何在更新密码时测试密码长度

Ruby-rspec - 如何打印出测试(组)名称

ruby-on-rails - 如何使 capybara 使用路由助手

ruby-on-rails - Rails 迁移总是在模式中插入空格或更改列的顺序

ruby-on-rails-3 - 编写 RSpec 测试/示例的最佳实践是什么?编写示例来测试正面/肯定、负面或两者?

ruby-on-rails - Rspec 2 - --使用guard格式化html

ruby - 将 SitePrism 与 Rspec 和 Capybara 功能规范一起使用