Ruby on rails Rspec测试获胜';使用赋值()进行t传递

Ruby on rails Rspec测试获胜';使用赋值()进行t传递,ruby-on-rails,ruby,testing,rspec,controller,Ruby On Rails,Ruby,Testing,Rspec,Controller,这是我的当前用户\u controller\u spec.rb文件 require 'spec_helper' describe UsersController do render_views . . . describe "success" do before(:each) do @attr = { :name => "New User", :email => "user@example.com", :passwor

这是我的当前用户\u controller\u spec.rb文件

require 'spec_helper'

describe UsersController do
  render_views
  .
  .
  .
  describe "success" do

   before(:each) do
    @attr = { :name => "New User", :email => "user@example.com",
              :password => "foobar", :password_confirmation => "foobar" }
  end

  it "should create a user" do
    lambda do
      post :create, :user => @attr
    end.should change(User, :count).by(1)
  end

  it "should redirect to the user show page" do
    post :create, :user => @attr
    response.should redirect_to(user_path(assigns(:user)))
   end
  end
 end
end
当我运行此命令时,会得到以下结果:

 Failures:

1) UsersController POST 'create' success should redirect to the user show page
 Failure/Error: response.should redirect_to(user_path(user))
 ActionController::RoutingError:
   No route matches {:action=>"show", :controller=>"users"}
 # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.should be_valid
  response.should redirect_to(user_path(user))
end
当我运行以下命令时:

 Failures:

1) UsersController POST 'create' success should redirect to the user show page
 Failure/Error: response.should redirect_to(user_path(user))
 ActionController::RoutingError:
   No route matches {:action=>"show", :controller=>"users"}
 # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.should be_valid
  response.should redirect_to(user_path(user))
end
我得到:

1) UsersController POST 'create' success should redirect to the user show page
     Failure/Error: user.should_not be_blank
       expected blank? to return false, got true
     # ./spec/controllers/users_controller_spec.rb:94:in `block (4 levels) in <top (required)>'
1)UsersController发布“创建”成功应重定向到用户显示页面
失败/错误:user.u不应为空
期望空白?返回假,得到真
#./spec/controllers/users\u controller\u spec.rb:94:in'block(4级)in'
你的打字错误

response.should redirect_to(user_path(:user))
应该是

response.should redirect_to(user_path(user))
编辑: 请尝试使用以下各项检查用户是否有效:

it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.should be_valid
  response.should redirect_to(user_path(user))
end
我知道它适用于上一个测试用例。。。但仍然值得在这里广泛检查至少一次。你可以在确定的情况下删除所有废话。

在测试中:

 it "should redirect to the user show page" do
    @user = Factory(:user)
    post :create, :user => @attr
    user = assigns(:user)
    response.should redirect_to(user_path(@user))
  end
您的
@attr
哈希与@user不同。您正在创建属性为@attr的用户,并断言它应该重定向到@user show(这与创建的属性不同)。改为:

 it "should redirect to the user show page" do
    post :create, :user => @attr
    user = assigns(:user)
    response.should redirect_to(user_path(user))
  end

尝试使用带括号而不是括号的
赋值

分配[:用户]

()


编辑

在测试控制器操作时,我经常使用的一种模式是确保分配了关联视图所需的变量。比如说,

it "should assign an @user variable" do
  post :create, :user => @attr
  assigns[:user].should_not be_nil
  assigns[:user].should be_kind_of(User)
end

然后我得到以下错误:#的“未定义的局部变量或方法
user”。我不确定你的建议是否让我离让它工作更近或更远。请参阅上面的更新代码,我清理了它并删除了我的测试代码。它是否仍在崩溃?如果是这样的话,我在上面的回答中有更多的建议。好的。。。这很奇怪。在“创建”操作中,执行一个
放置参数[:user]。检查
我是将其放置在控制器中还是测试中?我把它放在我的控制器中,但我不确定放在哪里。我知道我的@attr与我的
@user
不同。我已经清理了代码并删除了测试代码。希望这能澄清问题。不过还是一样的错误。谢谢你的建议!您是否尝试过:
response.assigns(:user.email.should==@attr[:email])
测试类型。如果您使用@attr发布到:create,则会出现这种情况。规范“应创建用户”正在通过?请尝试在“不应为”空白断言之前,按照@Taryn的建议在用户上打印错误。用户对象上有错误吗?是的,有。您使用的是什么版本的RSpec
gem list rspec
我不明白为什么它
应该分配@user变量
(检查assigns[:user]是否为nil)是通过的,但是spec直接在它为assigns[:user]返回nil之后。也许值得一提的是,这里有一个冗余测试<代码>分配[:用户]。应该是(用户)的种类。
测试
分配[:用户]。不应该是零。
。在重构过程中,您可能会丢失
assigns[:user]这一行。