【问题标题】:How to test text on rendered view?如何在渲染视图上测试文本?
【发布时间】:2013-01-25 06:28:18
【问题描述】:

这是我的测试:

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)>'

【问题讨论】:

    标签: rspec ruby-on-rails-3.2 rspec2


    【解决方案1】:

    在您的视图模板中,您实际上是否有正在检查的文本?错误似乎是它实际上无法找到文本。

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

    【讨论】:

      【解决方案2】:

      试试这个:

      response.should have_content("Simulate Circuits Online")
      

      【讨论】:

      • 我搜索了 RSpec 的文本匹配器,但是,我找不到任何东西,你能告诉我在哪里阅读吗?
      【解决方案3】:

      您还可以使用 DOM 元素的属性和内容。

      expect(page).to have_selector 'h1', :text => 'Your text here'
      

      此方法不会检索任何弃用警告。

      【讨论】:

        猜你喜欢
        • 2015-06-24
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多