English 中文(简体)
3 REPEC控制器测试故障/错误:
原标题:Rails 3 RSpec controller testing Failure/Error:

我正在测试我的铁路3.2应用程序 当我尝试测试我的控制器时 我遇到了一个错误

这是我的控制器

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

这是我的控制器 spec

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

I did this tutorial http://www.codethinked.com/rails-3-baby-steps-part-4 and I did not face with any problems. How should I fix it? What does this error mean?

最佳回答

我想这与控制器中当前用户返回的部件集的范围界定有关。 在控制器中, 您正在调用一个部件对象收藏的 < code> all 方法( 部件类的属性), 在测试中您正在跳过部件类的 < code> all 方法。 您可以通过修改控制器方法来测试这一理论, 只需调用 < code> 元件。 all 。 我知道这不是您想要的, 但是它会确认问题所在 。

问题回答

暂无回答




相关问题
uninitialized constant

I m trying to do this tutorial-> http://netbeans.org/kb/docs/ruby/rapid-ruby-weblog.html BUT its giving me this error: NameError in PostsController#index uninitialized constant PostsController::...

Rails - Too much logic in views?

I have an application used by several organizations and I want to check that users of one domain (a.domain.com) cannot edit users of another domain (b.domain.com). My question is where to put the ...

Controller specs in isolation mode and render :update

I am using RSpec for writing my controller tests/specs. I faced the problem, that the following code gets rendered: render :update do |page| page[ middle_content ].replace_html :partial => "...

registering event listeners in a controller

I m working on creating a simple MCV application, in order to understand MVC better. The problem I m having is registering event listeners. The way I see MVC is that the view dispatches events, the ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签