Eric Yang
Eric Yang

Reputation: 1889

rspec testing for content

I'm using Rspec to test the contents of a view in my Controller spec. I'm trying to test that all Product entries have their descriptions displayed on the page.

describe StoreController do
  render_views

  describe "GET 'index'" do

    before(:each) do
      get :index
    end

    it "should display the product list" do
      Product.all.each do |product|
        response.should have_selector("p", :content => product.description)
      end
    end

  end
end

This doesn't seem to work, however, as the test passes regardless of what's in the view. Still very new to Rails, so it's probable that I'm doing something completely wrong here. How can I make the code test for the presence of each product description in the StoreController index view?

Upvotes: 1

Views: 2304

Answers (2)

Eric Yang
Eric Yang

Reputation: 1889

I figured it out. The problem was that my test database was not populated with any Products, so the each block was never executing. Fixed it by using Factory Girl to make sure there was data in the test database beforehand.

Upvotes: 0

user483040
user483040

Reputation:

Personally I wouldn't test the contents of the view in the controller. I'd just test the outgoing output of the controller actions and any support methods. I'd put view related tests into the view specs.

If you look in the ones that are generated by rails you should see some examples of how to assert content there.

Upvotes: 4

Related Questions