Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test text on rendered view?

Here is my test:

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

but, I got:

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'

So, what's the right way to test the text rendered?

EDIT If I try response.should have_content("My text ..")

I get

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)>'
like image 891
simo Avatar asked Jan 25 '13 06:01

simo


3 Answers

In your view template do you actually have the text you are checking for? The error seems to be that it couldn't actually find the text.

Also, try using response.should have_text.

like image 128
PericlesTheo Avatar answered Jan 03 '23 16:01

PericlesTheo


Try this:

response.should have_content("Simulate Circuits Online")
like image 33
Jim Stewart Avatar answered Jan 03 '23 16:01

Jim Stewart


You can also use the DOM element's attributes and content.

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

This method doesn't retrieve any deprecations warnings.

like image 38
Sîrbu Nicolae-Cezar Avatar answered Jan 03 '23 16:01

Sîrbu Nicolae-Cezar