rspec view spec and dealing with current_user
Posted: March 30th, 2012 | Author: jgeiger | Filed under: ruby | Tags: rails, rspec, ruby | No Comments »I was trying to learn about view specs since doing everything in Cucumber has been considered to be a bit of overkill recently. I was looking for a solution to deal with current_user and this is what I’m using.
items/show.html.haml
|
1 2 3 4 |
.content - if @item.user == current_user Hello! This is the rest of the content. |
show.html_spec.rb
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
require 'spec_helper' describe "items/show" do before do @user = mock_model(User) view.stub(:current_user).and_return(@user) end context "when logged in as the item owner" do it "should display 'Hello!'" do assign(:item, stub_model(Item, user: @user)) render rendered.should have_content("Hello!") end end context "when logged in as anyone else" do it "should not display the 'Hello!' link" do assign(:item, stub_model(Item, user: nil)) render rendered.should_not have_content("Hello!") end end end |
Leave a Reply