Rspec organization
I am currently using rspec with cancan. I realized that littering permission control test cases all over my controllers spec files is extremely messy. Basically, almost every controller spec files of mine has something along the lines:
describe "failure" do
it { get :new }
it { get :edit, :id => @deal }
it { get :update, :id => @deal }
it { get :destroy, :id => @deal }
after(:each) do
response.should_not be_success
response.should redirect_to(root_path)
flash[:error].should == "Permission denied."
end
end
end
I have 4 roles in my system and this definitely makes organization a much more difficult task.
Since all of these tests are related to permission control/ACL, I tried putting them all in one file rspec/models/ability_spec.rb
right now, my ability_spec looks like this:
describe "cancan" do
it "failure" do
@ability.should_not be_able_to(:all, Factory(:purchase))
@ability.should_not be_able_to(:all, Factory(:user))
@ability.should_not be_able_to(:all, Visit)
end
end
I am getting the following error:
6) Ability consumers deals failure
Failure/Error: it { get :destroy, :id => @deal }
NoMethodError:
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_2::Nested_2:0x007fd73209a270>
# ./spec/models/ability_spec.rb:46:in `block (5 levels) in <top (required)>'
I know I am not supposed to put controller get/post in this file. Is there a way to do this fo开发者_如何学Pythonr the sake of simplifying testing for my permission related tests?
Take a look at RSpec's shared examples and see if you can pull anything out into a shared example group:
http://relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
精彩评论