Organización Rspec

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

Estoy teniendo el siguiente 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 for the sake of simplifying testing for my permission related tests?

preguntado el 28 de agosto de 11 a las 01:08

Unrelated to your question, but your specs are not very readable, they do not tell me (as a human) what you're describing. it { get :some_action } describes nothing. You should use natural language expressions to describe your code. -

1 Respuestas

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

Respondido 28 ago 11, 08:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.