मुझे नियंत्रक के पास आरएसपीसी परीक्षण प्राप्त करने में कठिनाई हो रही है। मैं परीक्षण करना चाहता हूं कि POST कार्रवाई कार्य करता है। मैं रेल (3.0.3), कैनकैन (1.4.1) का उपयोग कर रहा हूँ, वसीयत (1.1.5), rspec (2.3.0)rspec नियंत्रक POST के साथ परीक्षण नहीं कर सकता है कार्रवाई (devise और cancan)
मॉडल मृत सरल
class Account < ActiveRecord::Base
attr_accessible :name
end
नियंत्रक मानक है साथ ही (सीधे मचान से बाहर)
class AccountsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
load_and_authorize_resource
...
def create
@account = Account.new(params[:account])
respond_to do |format|
if @account.save
format.html { redirect_to(@account, :notice => 'Account was successfully created.') }
format.xml { render :xml => @account, :status => :created, :location => @account }
else
format.html { render :action => "new" }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end
end
और rspec परीक्षण मैं पारित करने के लिए चाहते हैं (शीर्षक बहाना है, शायद नहीं सबसे उपयुक्त एक)
it "should call create on account when POST create is called" do
@user = Factory.create(:user)
@user.admin = true
@user.save
sign_in @user #this is an admin
post :create, :account => {"name" => "Jimmy Johnes"}
response.should be_success
sign_out @user
end
फिर भी सब मुझे मिलता
AccountsController get index should call create on account when POST create is called
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/accounts_controller_spec.rb:46
अन्य कार्यों का परीक्षण किया जा सकता है और पारित करना है (अर्थात नई प्राप्त)
यहाँ परीक्षण है प्राप्त नई
it "should allow logged in admin to call new on account controller" do
@user = Factory.create(:user)
@user.admin=true
@user.save
sign_in @user #this is an admin
get :new
response.should be_success
sign_out @user
end
और पूरा होने के लिए यहां क्षमता फ़ाइल
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
कोई भी विचार है? मेरा अनुमान है कि मैं गलत rspec उम्मीद का उपयोग कर रहा हूं, क्योंकि कोड काम करता है (यह सिर्फ इतना है कि परीक्षण वांछित के रूप में नहीं करता है!)
zetetic , आपका जवाब उत्कृष्ट था। मुझे इसे पास करने के लिए कुछ tweeks करना था, लेकिन इसका सारांश यह था कि यह एक पुनर्निर्देशन होना चाहिए था। – Dimitris
मुझे लगता है कि यह उत्तर स्वीकार्य उत्तर होना चाहिए। –
मैंने इसे स्वीकृत उत्तर के रूप में वोट दिया क्योंकि अधिकांश लोगों को यह उत्तर सबसे उपयोगी लगता है – Dimitris