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:
describe “categories/index.html.erb” do
before(:each) do
view.stub(:is_admin).and_return(true)
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
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
How can I best write this?
Would using “:let” before each different spec (admin and non-admin)
be a good way to do it?
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
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.
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
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.