2011-01-03 9 views
6

मुझे नियंत्रक के पास आरएसपीसी परीक्षण प्राप्त करने में कठिनाई हो रही है। मैं परीक्षण करना चाहता हूं कि 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 उम्मीद का उपयोग कर रहा हूं, क्योंकि कोड काम करता है (यह सिर्फ इतना है कि परीक्षण वांछित के रूप में नहीं करता है!)

उत्तर

23

response.should be_success प्रतिक्रिया कोड 200-299 की सीमा में है तो सच हो जाता है। लेकिन निर्माण कार्य रीडायरेक्ट करता है, इसलिए प्रतिक्रिया कोड 302 पर सेट हो जाता है, इस प्रकार विफलता।

आप response.should redirect_to का उपयोग कर इसका परीक्षण कर सकते हैं। एक उदाहरण के लिए मानक RSpec नियंत्रक जनरेटर के उत्पादन में है, जो इस प्रकार दिखाई देंगे की जाँच करें:

it "redirects to the created account" do 
    Account.stub(:new) { mock_account(:save => true) } 
    post :create, :account => {} 
    response.should redirect_to(account_url(mock_account)) 
    end 
+0

zetetic , आपका जवाब उत्कृष्ट था। मुझे इसे पास करने के लिए कुछ tweeks करना था, लेकिन इसका सारांश यह था कि यह एक पुनर्निर्देशन होना चाहिए था। – Dimitris

+0

मुझे लगता है कि यह उत्तर स्वीकार्य उत्तर होना चाहिए। –

+0

मैंने इसे स्वीकृत उत्तर के रूप में वोट दिया क्योंकि अधिकांश लोगों को यह उत्तर सबसे उपयोगी लगता है – Dimitris

3

rspec परीक्षण है कि पारित करने के लिए (zetetic की सलाह के लिए धन्यवाद) था परीक्षण हो गया है:

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 
    account = mock_model(Account, :attributes= => true, :save => true) 
    Account.stub(:new) { account } 

    post :create, :account => {} 
    response.should redirect_to(account_path(account)) 
    sign_out @user 

end 
संबंधित मुद्दे