2012-05-25 4 views
0

레일즈 3.2 앱을 테스트 중이며 컨트롤러 테스트를 시도 할 때 오류가 발생했습니다. 레일 3 RSpec 컨트롤러 테스트 실패/오류 :

이 내 컨트롤러 사양

난 그냥 인덱스 페이지에서 데이터를 얻을 수 있다는 것을보고 싶어
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 

내 컨트롤러

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 

입니다. 그러나 나는이 튜토리얼 http://www.codethinked.com/rails-3-baby-steps-part-4했다

.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 

$rspec spec/controllers/widgets_controller_spec.rb을 실행할 때 명령 행에서이 오류를 본 나는 어떤 문제에 직면하지 않았다. 어떻게 수정해야합니까? 이 오류는 무엇을 의미합니까?

답변

1

나는 그것이 컨트롤러의 current_user에 의해 반환 된 위젯 세트를 범위 지정하는 것과 관련이 있다고 생각합니다. 컨트롤러에서 widget 객체 (Widget 클래스의 인스턴스) 컬렉션에 대해 all 메서드를 호출하고 있으며 테스트에서 위젯 클래스의 all 메서드를 스텁하고 있습니다. 컨트롤러 방법을 변경하여 간단히 Widget.all으로 전화하여이 이론을 테스트 할 수 있습니다. 나는 그것이 당신이 원하는 것을 알지 못하지만 이것이 이것이 문제임을 확인해 줄 것입니다.

+0

네가 맞습니다. 나는 그의 ID와 함께 테스트 페이지 current_user를 추가하는 문제를 해결했다. – ndrx42

관련 문제