2010-06-11 17 views
5

के साथ पहले_फिल्टर को दबाकर मुझे समझ में आ रहा है कि मैं इस नियंत्रक विधि :load_user को क्यों दबा सकता हूं, क्योंकि मेरे सभी परीक्षण विफल होने पर :load_user के वास्तविक कार्यान्वयन को बदलने और @user के उदाहरण को बदलने में विफल रहता है।आरएसपीसी

क्या कोई देख सकता है कि क्यों मेरा स्टब (controller.stub!(:load_user).and_return(@user)) वास्तव में कॉल करने में विफल रहता है जब आरएसपीसी नियंत्रक से अनुरोध करता है?

require 'spec_helper' 

describe TasksController do 

    before(:each) do 
    @user = Factory(:user) 
    sign_in @user 
    @task = Factory(:task) 
    User.stub_chain(:where, :first).and_return(@user) 
    controller.stub!(:load_user).and_return(@user) 
    end 

    #GET Index 
    describe "GET Index" do 

    before(:each) do 
     @tasks = 7.times{Factory(:task, :user => @user)} 
     @user.stub!(:tasks).and_return(@tasks) 
    end 

    it "should should find all of the tasks owned by a user" do 
     @user.should_receive(:tasks).and_return(@tasks) 
     get :index, :user_id => @user.id 
    end 

    it "should assign all of the user's tasks to the view" do 
     get :index, :user_id => @user.id 
     assigns[:tasks].should be(@tasks)  
    end 
    end 

    #GET New 
    describe "GET New" do 

    before(:each) do 
     @user.stub_chain(:tasks, :new).and_return(@task) 
    end 

    it "should return a new Task" do 
     @user.tasks.should_receive(:new).and_return(@task) 
     get :new, :user_id => @user.id 
    end 
    end 

    #POST Create 
    describe "POST Create" do 

    before(:each) do 
     @user.stub_chain(:tasks, :new).and_return(@task) 
    end 

    it "should create a new task" do 
    @user.tasks.should_receive(:new).and_return(@task) 
     post :create, :user_id => @user.id, :task => @task.to_s 
    end 

    it "saves the task" do 
     @task.should_receive(:save) 
     post :create, :user_id => @user.id, :task => @task 
    end 

    context "when the task is saved successfully" do 

     before(:each) do 
     @task.stub!(:save).and_return(true) 
     end 

     it "should set the flash[:notice] message to 'Task Added Successfully'"do 
     post :create, :user_id => @user.id, :task => @task 
     flash[:notice].should == "Task Added Successfully!" 
     end 

     it "should redirect to the user's task page" do 
     post :create, :user_id => @user.id, :task => @task 
     response.should redirect_to(user_tasks_path(@user.id)) 
     end 
    end 

    context "when the task isn't saved successfully" do 

     before(:each) do 
     @task.stub(:save).and_return(false) 
     end 

     it "should return to the 'Create New Task' page do" do 
     post :create, :user_id => @user.id, :task => @task 
     response.should render_template('new') 
     end 
    end 
    end 

    it "should attempt to authenticate and load the user who owns the tasks" do 

    context "when the tasks belong to the currently logged in user" do 

     it "should set the user instance variable to the currently logged in user" do 
     pending 
     end 

    end 

    context "when the tasks belong to another user" do 

     it "should set the flash[:notice] to 'Sorry but you can't view other people's tasks.'" do 
     pending 
     end 

     it "should redirect to the home page" do 
     pending 
     end 
    end 
    end 
end 

class TasksController < ApplicationController 
    before_filter :load_user 

    def index 
    @tasks = @user.tasks 
    end 

    def new 
    @task = @user.tasks.new 
    end 

    def create 
    @task = @user.tasks.new 
    if @task.save 
     flash[:notice] = "Task Added Successfully!" 
     redirect_to user_tasks_path(@user.id) 
    else 
     render :action => 'new' 
    end 
    end 

    private 

    def load_user 
    if current_user.id == params[:user_id].to_i 
     @user = User.where(:id => params[:user_id]).first 
    else 
     flash[:notice] = "Sorry but you can't view other people's tasks." 
     redirect_to root_path 
    end 
    end 
end 

क्या कोई देख सकता है कि मेरा स्टब क्यों काम नहीं करता है? जैसे मैंने कहा, मेरे परीक्षण केवल तभी पास होते हैं जब मैं सुनिश्चित करता हूं कि load_user काम करता है, यदि नहीं, तो मेरे सभी परीक्षण विफल हो जाते हैं जो मुझे लगता है कि आरएसपीईसी मैंने बनाए गए स्टब का उपयोग नहीं कर रहा है।

+0

क्षमा करें मेरे पास असली जवाब नहीं है, लेकिन सुनिश्चित करें कि आप अपने परीक्षणों में नियंत्रक को दबा रहे हैं और वास्तव में कोड निष्पादित करने वाले नियंत्रक एक ही उदाहरण हैं। आप सुनिश्चित करने के लिए अपने 'object_id' की जांच कर सकते हैं। – x1a4

उत्तर

8

एक अभिकथन करके ठूंठ काम करता है बाहर load_user टूट जाता है अपने परीक्षण छोटा करते क्योंकि विधि neuters यह छोटा करते सत्यापित करने का प्रयास कर सकते हैं। जब नियंत्रक load_user पर कॉल करता है, तो यह अब आपका मूल कोड नहीं चला रहा है। यह अब and_return(...) में जो कुछ भी निर्दिष्ट है, उसे वापस लौटा रहा है (जो ActionController कॉलबैक स्टैक पर वापस लौटा रहा है, जो false के अलावा कुछ भी अनदेखा करता है)।

आपका नियंत्रक कोड उस विधि के वापसी मूल्य का उपयोग नहीं कर रहा है; यह परिवर्तनीय तत्काल का उपयोग के भीतर कर रहा है। चूंकि load_user विधि के लिए मूल कोड नहीं चल रहा है, इसलिए @user इंस्टेंस वैरिएबल कभी भी तत्काल नहीं है। (@user आपके परीक्षणों में परिवर्तनीय केवल आपके परीक्षणों के लिए दृश्यमान है।)

लेकिन आपके पास अन्य सभी स्टब्स के साथ, मुझे कोई कारण नहीं दिख रहा है कि आपको load_user को स्टब करने की आवश्यकता क्यों है। जब तक आप current_user को @user (जो मुझे लगता है कि sign_in विधि में किया जा रहा है) लौटने के लिए, तब कोई आवश्यकता नहीं होनी चाहिए।

+0

यह एक महान स्पष्टीकरण है, अगर मैं इसे और अधिक वोट दे सकता हूं। बहुत बहुत धन्यवाद, @user बनाम @user को लौटने के बारे में स्पष्टीकरण ने वास्तव में इसे मेरे लिए क्लिक करने में मदद की। – TheDelChop

2

आप भी ऐसे ही

controller.current_user.should == @user