On Dec 12, 2011, at 5:30 AM, Gordon wrote:

> hi guys,
> 
> In my index view specs for a given resource, I have successfully
> written it for admin users.
> 
> For the given resource, when admin users are looking at the index
> page, they should see
> -a link to add a new resource object
> -links to delete/edit/show for each resource object created
> 
> My index view specs does this perfectly and it reads:
> 
> ------- file: begin -----------------------
> 
> 
> 192-168-1-4:categories anexiole$  cat index.html.erb_spec.rb
> require 'spec_helper'
> 
> describe "categories/index.html.erb" do
>  before(:each) do
>       view.stub(:is_admin).and_return(true)

^^ I've been in the habit of using view.stub(:is_admin => true).

Also, the Ruby idiom for questions like "is this being viewed by an admin" is a 
Ruby predicate - a method name with a question mark at the end:

  view.stub(:admin? => true)

Names like is_admin are usually holdovers from Java :)

>       assign( :categories, [
>                       FactoryGirl.create(:category_intakes),
>                       FactoryGirl.create(:category_audio),
>               ]
>       )
>  end
> 
>  it "renders a list of categories" do
>    render
>    assert_select 'tr>td', :text => 'intakes and filters'.to_s, :count => 1
>    assert_select 'tr>td', :text => 'audio'.to_s, :count => 1

^^ 'intakes and filters' and 'audio' are already strings, so you don't need 
.to_s.

>  end
> 
>       it 'renders an interface with the new link' do
>               render
>               rendered.should contain ('New Category')
>       end
> 
> end
> 
> ------- file: end    -----------------------
> 
> Now, I would then like to put more specs in the index view spec.
> 
> When a non-admin user (ie someone who has not logged in) looks at the
> index page, the user
> should NOT see
> -a link to add a new resource object
> -links to delete/edit/show for each resource object created
> 
> 1) How can I best write this?

Best is subjective. One way you can write it is:

context "viewed by a non-admin" do
  before do
    view.stub(:admin? => false)
    # set up assigns
  end
  it "should not see xxx" do
    render
    assert_select selector, .. :count => 0
    # or, if you're using Capybara
    rendered.should_not have_tag(selector)
  end
end
    

> 2) Would using ":let" before each different spec (admin and non-admin)
> be a good way to do it?

let() is for creating objects, which is not what you're doing here - you're 
simulating different logins. I'd stick with stubbing :admin? in the before 
declaration.

Cheers,
David

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to