2012-03-26 174 views
3

我正在使用Rspec和Rails 3進行測試。我已經測試過我的模型和幫手,但是我對如何開始測試控制器感到迷茫。幾乎所有我在我的控制器行動的數據可以用下面的這些例子拉住:使用RSpec測試Rails控制器 - 如何測試:current_account.projects

@services = current_account.services 

@projects = current_person.projects 

@projects = current_account.projects.active 
# this is really @projects = current_person.account.projects.active) 

我似乎無法找到如何測試真實拉這樣的數據的例子。我找到的所有例子都沒有限定在某個帳戶或某個人的範圍內。任何人都可以給我一個關於如何模擬或存根這種安排的文章嗎?這是否表示整個方法不正確?下面,我包含了一個完整的示例控制器。

任何幫助將不勝感激。

謝謝, 大衛

class ServicesController < ApplicationController 
    # Run authorizations 
    filter_resource_access 

    # Respond to ... 
    respond_to :html, :xml, :json 
    respond_to :js, :only => [:new, :create, :edit, :update, :destroy] 

    # GET /services 
    # GET /services.xml 
    def index 
    @services = current_account.services.order("name").paginate(:page => params[:page]) 

    respond_with(@services) 
    end 

    # GET /services/1 
    # GET /services/1.xml 
    def show 
    @service = current_account.services.find(params[:id]) 

    respond_with(@service) 
    end 

    # GET /services/new 
    # GET /services/new.xml 
    def new 
    @service = current_account.services.new 

    respond_with(@service) 
    end 

    # GET /services/1/edit 
    def edit 
    @service = current_account.services.find(params[:id]) 

    respond_with(@service) 
    end 

    # POST /services 
    # POST /services.xml 
    def create 
    @service = current_account.services.new(params[:service]) 

    if @service.save 
     # flash[:notice] = 'A service was successfully created.' 
    end 

    respond_with(@service, :location => services_url) 
    end 

    # PUT /services/1 
    # PUT /services/1.xml 
    def update 
    @service = current_account.services.find(params[:id]) 

    if @service.update_attributes(params[:service]) 
     # flash[:notice] = 'The service was successfully updated.' 
    end 

    respond_with(@service, :location => services_url) 
    end 

    # DELETE /services/1 
    # DELETE /services/1.xml 
    def destroy 
    @service = current_account.services.find(params[:id]) 

    if @service.destroy 
     flash[:notice] = "The service was successfully deleted." 
    else 
     flash[:warning] = @service.errors.full_messages.inject("") { |acc, message| acc += message } 
    end 

    respond_with(@service) 
    end 
end 

------ UPDATE

由於Xaid的解決方案,我能得到一個解決方案:

context "logged_in" do 
    before(:each) do 
     @current_account = Factory.create(:account) 
     controller.stub!(:current_account).and_return(@current_account) 

     @services = FactoryGirl.create_list(:service, 10, :account => @current_account) 
     @services << @current_account.services.first 
     @current_account.services.stub!(:all).and_return(@services) 
    end 


    # INDEX 
    describe "GET services" do 
     before(:each) do 
     get :index 
     end 

     it "should set @services when accessing GET /index" do 
     assigns[:services].should == @services 
     end 

     it "should respond with success" do 
     response.should be_success 
     end 
    end 
    end 

回答

4

不能使用這樣的事情來測試你的'索引'行動

describe "GET 'index'" do 
    before(:each) do 
    @user = FactoryGirl.create(:user) 
    controller.stub!(:current_user).and_return(@user) 
    @services = FactoryGirl.create_list(:service, 10, :user => @user) 
    @user.services.stub!(:all).and_return(@services) 
    end 

    it "should return a list of services" do 
    get :index 
    assigns(:services).should == @services 
    end 
end 

如果我正確理解您的問題,您應該能夠存根current_user.services(或項目)並使其返回一些已知值(由FactoryGirl在我的示例中生成),並檢查存儲在您的操作中的值(例如,您的'索引'操作中的@services)。

+0

謝謝!這是我第一次使用存根,所以我對從哪裏開始有點困惑。我會給你一個鏡頭,讓你知道它是如何爲我工作的。 – 2012-03-27 03:26:22