RSpec integration testing failure when using 'load_and_authroize_resource' in controllers
As soon as I added the method 'load_and_authorize_resource' to m controllers a bunch of my RSpec tests fail. As I had some outstanding failures I proved to think I have confirmed that its this method by generating a new resource 'purchases' via scaffolding. I am using home grown authentication as per the Hartl tutorial.
With the scaffold in place running 'rspec spec/requests/purchases_spec.rb' fails unless I comment out the call to the method in the purchases controller.
How do I successfully test when using this method?
Here is my controller:
class PurchasesController < ApplicationController
before_filter :logged_in_user
load_and_authorize_resource
# GET /purchases
# GET /purchases.json
def index
@purchases = Purchase.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @purchases }
end
end
# GET /purchases/1
# GET /purchases/1.json
def show
@purchase = Purchase.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @purchase }
end
end
# GET /purchases/new
# GET /purchases/new.json
def new
@purchase = Purchase.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @purchase }
end
end
# GET /purchases/1/edit
def edit
@purchase = Purchase.find(params[:id])
end
# POST /purchases
# POST /purchases.json
def create
@purchase = Purchase.new(params[:purchase])
respond_to do |format|
if @purchase.save
format.html { redirect_to @purchase, notice: 'Purchase was successfully created.' }
format.json { render json: @purchase, status: :created, location: @purchase }
else
format.html { render action: "new" }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
# PUT /purchases/1
# PUT /purchases/1.json
def update
@purchase = Purchase.find(params[:id])
respond_to do |format|
if @purchase.update_attributes(params[:purchase])
format.html { redirect_to @purchase, notice: 'Purchase was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
@purchase = Purchase.find(params[:id])
@purchase.destroy
respond_to do |format|
format.html { redirect_to purchases_url }
format.json { head :no_content }
end
end
end
Here is the request spec:
require 'spec_helper'
describe "Purchases" do
let(:user) { FactoryGirl.create(:user) }
let(:patients_role) { FactoryGirl.create(:patients_role) }
let(:assignment) { FactoryGirl.create(:user, :patients_role)}
before do
login user
define_permission!(patients_role.id, :purchases, "read")
load_and_authorize_resource
end
describe "GET /purchases" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get purchases_path
# load_and_authorize_resource
response.status.should be(200)
end
end
describe "GET /purchases/new page" do
it "Should display the New Purchases Page" do
get new_purchase_path
response.status.should be(200)
end
end
end
Does this fail because its a scaffold spec as none of the requests specs that I create utilising my understanding of this as applied in the Hartl tutorial?
Here is my authorization_helper.rb file under spec/support.
module AuthorizationHelpers
def define_permission!(role_id, resource, action)
Permission.create!(:role_id => role_id,
:subject_class => resource,
:action => action
)
end
end
RSpec.configure do |c|
c.include AuthorizationHelpers
end
Being relatively new to RSpec/Rails/Ruby it may be I've misunderstood what needs to be tested.
In my application this all works, its just my testing that is causing the problem. I dearly want to learn good habits as I come from a previous technology area where testing was an after-thought on big programmes of work, so the emphasis that this technology places on up-front testing (TDD/BDD) is a breath of fresh air, but I'm at a point where it would be easy to turn ones back on tests that do not work when the application is working, as there is more to deliver and its holding me up.
Any suggestions will be greatly appreciated and worked through.
Here is the RSpec error:
FF
Failures:
1) Purchases GET /purchases works! (now write some real specs)
Failure/Error: response.status.should be(200)
expected #<Fixnum:401> => 200
got #<Fixnum:605> => 302
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
'actual.should eq(expected)' if you don't care about
object identity in this example.
# ./spec/requests/purchases_spec.rb:19:in `block (3 levels) in <top (required)>'
2) Purchases GET /purchases/new page Should display the New Purchases Page
Failure/Error: response.status.should be(200)
expected #<Fixnum:401> => 200
got #<Fixnum:605> => 302
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
'actual.should eq(expected)' if you don't care about
object identity in this example.
# ./spec/requests/purchases_spec.rb:26:in `block (3 levels) in <top (required)>'
Finished in 0.97432 seconds
2 examples, 2 failures
Dear submitter, Since cancan/raynB hasn't been active for more than 6 months and no body else then ryam himself has commit permissions the cancan project is on a stand still. Since cancan has several issues including missing support for rails 4 cancan is moving forward to cancancan. More details on: #994
If your feel that your pull request or bug is still applicable (and hasn't been merged in to cancan) it would be really appreciated if you would resubmit it to cancancan (https://github.com/cancancommunity/cancancan)
We hope to see you on the other side!
hello i am having some issue an unexpected issue with integration testing: this is my autotest error trace: [sample_app (filling-in-layout)]$ rspec spec/ FFF..........
Failures:
-
LayoutLinks should have a Help page at '/help' Failure/Error: response.should have_selector('title', :content=>"Help") expected following output to contain a
Help tag:./spec/requests/layout_links_spec.rb:18:in `block (2 levels) in <top (required)>'
-
LayoutLinks should have a About page at '/about' Failure/Error: response.should have_selector('title', :content=>"About") expected following output to contain a
About tag:./spec/requests/layout_links_spec.rb:14:in `block (2 levels) in <top (required)>'
-
LayoutLinks should have a Contact page at '/contact' Failure/Error: response.should have_selector('title', :content=>"Contact") expected following output to contain a
Contact tag:./spec/requests/layout_links_spec.rb:10:in `block (2 levels) in <top (required)>'
Finished in 0.21778 seconds 13 examples, 3 failures
Failed examples:
rspec ./spec/requests/layout_links_spec.rb:17 # LayoutLinks should have a Help page at '/help' rspec ./spec/requests/layout_links_spec.rb:13 # LayoutLinks should have a About page at '/about' rspec ./spec/requests/layout_links_spec.rb:9 # LayoutLinks should have a Contact page at '/contact'
Note that these links do exits when i visit /help or /about, /contact but it is complaining that i dont have those pages. Can sombody please help on this issue?