2012-05-25 101 views
0

我正在測試我的Rails 3.2應用程序,當我試圖測試我的控制器時遇到錯誤。Rails 3 RSpec控制器測試失敗/錯誤:

這是我的控制器

class WidgetsController < ApplicationController 
    #skip_before_filter :authenticate, :only => [:new, :create] 
    before_filter :authenticate, :except=>[:disabled] 

    def index 
    @widgets = current_user.widgets.all 
    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @widgets } 
    end 
    end 

    def new 
    @widget = Widget.new 
    end 

    def create 
    @widget = Widget.new(params[:widget]) 
    @widget.user_id = current_user.id 
    @widget.score = 0 
    @widget.total_score = 0 
    @widget.click_number = 0 
    @widget.average = 0 
    respond_to do |format| 
     if @widget.save 
     format.html { redirect_to edit_widget_path(@widget), notice: 'Widget was successfully created.' } 
     format.json { render json: @widget, status: :created, location: @widget } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @widget.errors, status: :unprocessable_entity } 
     raise 'there is an error when creation' 
     end 
    end 
    end 

    def show 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    end 

    def edit 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    end 

    def update 
    @widget = Widget.find_by_uuid(params[:uuid]) 
    respond_to do |format| 
     if @widget.update_attributes(params[:widget]) 
     format.html { redirect_to edit_widget_path(@widget), notice: 'Widget was successfully updated.' } 
     format.json { render json: @widget, status: :created, location: @widget } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @widget.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @widget = Widget.find_by_uuid(params[:uuid]).destroy 
    redirect_to widgets_path 
    end 

    #generate widget 
    def generate 
    respond_to do |format| 
     format.js {} 
    end 
    rescue 
    #TODO add widget not found page 
    render :template => 'application/widget_not_found', :status => :not_found 
    end 



    protected 

    def authenticate 
    unless current_user 
     redirect_to root_url 
    end 
    end 

end 

這是我的控制器規格

require 'spec_helper' 

describe WidgetsController do 
    login_admin 

    describe "User" do 
    it "should have a current_user" do 
     subject.current_user.should_not be_nil 
    end 
    end 

    def mock_widget(stubs={}) 
    @mock_widget ||= mock_model(Widget, stubs).as_null_object 
    end 

    describe "GET index" do 
    it "assigns all widgets as @widgets" do 
     Widget.stub(:all) { [mock_widget] } 
     get :index 
     assigns(:widgets).should eq([mock_widget]) 
    end 
    end 

end 

我只是想看看我能得到索引頁上的數據。然而,當我運行$rspec spec/controllers/widgets_controller_spec.rb

.F 

Failures: 

    1) WidgetsController GET index assigns all widgets as @widgets 
    Failure/Error: assigns(:widgets).should eq([mock_widget]) 

     expected: [#<Widget:0x3ffb0c3c9d70 @name="Widget_1001">] 
      got: [] 

     (compared using ==) 

     Diff: 
     @@ -1,2 +1,2 @@ 
     -[#<Widget:0x3ffb0c3c9d70 @name="Widget_1001">] 
     +[] 
    # ./spec/controllers/widgets_controller_spec.rb:20:in `block (3 levels) in <top (required)>' 

Finished in 0.11937 seconds 
2 examples, 1 failure 

Failed examples: 

rspec ./spec/controllers/widgets_controller_spec.rb:17 # WidgetsController GET index assigns all widgets as @widgets 

我做這個教程http://www.codethinked.com/rails-3-baby-steps-part-4我看到的命令行這個錯誤,我沒有任何問題,面對的問題。 我應該如何解決它?這個錯誤是什麼意思?

回答

1

我想它與控制器中由current_user返回的一組widgets有關。在控制器中,您正在對Widget對象集合(Widget類的實例)調用all方法,並且在測試中,您正在對Widget類的all方法進行存根。你可以通過改變你的控制器方法來簡單地調用Widget.all來測試這個理論。我知道這不是你想要的,但它會證實這是問題所在。

+0

是的你是對的。我解決了這個問題,用他的id添加測試頁面current_user。 – ndrx42