1

我有以下控制器 RSpec 测试,但是当我尝试执行以下代码时,它应该可以工作:

require 'spec_helper'
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')


describe UsersController do
  include Devise::TestHelpers #Include devise test helpers
  render_views # Render devise views

  describe "GET 'show'"

  before(:each) do
    @user = User.find(params[:id])
    @attr = {:initials => 'EU', :name => 'Example', :full_name => 'Example User',
             :email => 'user@example.com', :password => 'password', :password_confirmation => 'password'

             #@attr = User.all
    }
  end

  it 'should be successful when showing OWN details' do
    get :show, :id => @attr
    response.should be_success
  end

  it 'should find the correct user' do
    get :show, :id => @attr
    assigns(@attr).should == @attr
  end
end

但是我得到以下输出:Undefined locacl variable or method 'params' for RSPRC我相信这个设置应该是正确的。

4

1 回答 1

2

I see a few things going on here.

Your calling params within the before statement but where is the id ever set? No where in this code do I see where the user id is ever actually determined/found. You should set @user explicitly using either an id that you know exists or doing something such as User.first.

Then instead of calling

get :show, :id => @attr 

you should be calling

get :show, :id => @user.id

Also I'm not sure why you need to include spec_helper twice. Line 2 should be able to be removed.

One more thing - assigns(@attr).should == @attr doesn't make any sense. It should be assigns(:attr).should == @attr. Otherwise you would be passing the value of @attr into the assigns method.

于 2013-04-19T16:52:43.200 回答