1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00

Update readme to include a sections on stubbing policy for view specs and external resources.

This commit is contained in:
Christian Nelson 2013-10-24 18:30:55 -07:00
parent 5d35dffc86
commit 05b9067df8

View file

@ -352,6 +352,8 @@ end
## RSpec
### Policy Specs
Pundit includes a mini-DSL for writing expressive tests for your policies in RSpec.
Require `pundit/rspec` in your `spec_helper.rb`:
@ -384,6 +386,36 @@ end
An alternative approach to Pundit policy specs is scoping them to a user context as outlined in this
[excellent post](http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/).
### View Specs
When writing view specs, you'll notice that the policy helper is not available
and views under test that use it will fail. Thankfully, it's very easy to stub
out the policy to have it return whatever is appropriate for the spec.
``` ruby
describe "users/show" do
before(:each) do
user = assign(:user, build_stubbed(:user))
controller.stub(:current_user).and_return user
end
it "renders the destroy action" do
allow(view).to receive(:policy).and_return double(edit?: false, destroy?: true)
render
expect(rendered).to match 'Destroy'
end
end
```
This technique enables easy unit testing of tricky conditionaly view logic
based on what is or is not authorized.
# External Resources
- [Migrating to Pundit from CanCan](http://blog.carbonfive.com/2013/10/21/migrating-to-pundit-from-cancan/)
- [Testing Pundit Policies with RSpec](http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/)
# License
Licensed under the MIT license, see the separate LICENSE.txt file.