Ruby on rails Rspec组织

Ruby on rails Rspec组织,ruby-on-rails,rspec,Ruby On Rails,Rspec,我目前正在使用rspec和cancan。我意识到在我的控制器规范文件中乱扔权限控制测试用例是非常混乱的。基本上,我的每个控制器规范文件都有如下内容: describe "failure" do it { get :new } it { get :edit, :id => @deal } it { get :update, :id => @deal } it { get :destroy, :id => @deal } after(

我目前正在使用rspec和cancan。我意识到在我的控制器规范文件中乱扔权限控制测试用例是非常混乱的。基本上,我的每个控制器规范文件都有如下内容:

  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
我在我的系统中有4个角色,这无疑使组织成为一项更加困难的任务

因为所有这些测试都与权限控制/ACL相关,所以我尝试将它们放在一个文件rspec/models/ability_spec.rb中

现在,我的能力规格如下:

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
我得到以下错误:

  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)>'
6)消费者处理故障的能力
失败/错误:它{get:destroy,:id=>@deal}
命名错误:
未定义的方法“get”#
#./spec/models/ability_spec.rb:46:in'block(5级)in'

我知道我不应该把controller get/post放在这个文件中。为了简化我的权限相关测试的测试,有没有办法做到这一点?

看看RSpec的共享示例,看看您是否可以将任何内容拉到共享示例组中:


与您的问题无关,但您的规格说明可读性不强,不能告诉我(作为人类)您在描述什么<代码>它{get:some_action}没有描述任何内容。您应该使用自然语言表达式来描述代码。