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(: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)>'
我知道我不应该将控制器 get/post 放入此文件中。有没有办法做到这一点,以简化我的权限相关测试的测试?
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 for the sake of simplifying testing for my permission related tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 RSpec 的共享示例,看看是否可以将任何内容放入共享示例组中:
http://relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
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