使用 allocate() 无法通过 Rspec 测试

发布于 2024-12-15 02:50:57 字数 2152 浏览 1 评论 0原文

这是我当前的 users_controller_spec.rb 文件

require 'spec_helper'

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

   before(:each) do
    @attr = { :name => "New User", :email => "[email protected]",
              :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)>'

这让我相信 :user 不是一个实际的对象。我如何测试这个以及如何将 :user 更改为 user_path 可以理解的对象。

预先感谢您的任何帮助。

更新:

def create
  @title = "Sign up"
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, :notice => "Signed Up!"
  else
    @title = "Sign up"
    render "new"
  end
end

当我运行以下命令时:

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)>'

Here is my current users_controller_spec.rb file

require 'spec_helper'

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

   before(:each) do
    @attr = { :name => "New User", :email => "[email protected]",
              :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

When I run this I get the following:

 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)>'

Which leads me to believe that :user isn't an actual object. How can I test this and how can I change :user into an object that user_path understands.

Thanks in advance for any help.

UPDATED:

def create
  @title = "Sign up"
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, :notice => "Signed Up!"
  else
    @title = "Sign up"
    render "new"
  end
end

When I run the following:

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

I get:

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)>'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月蹉跎了容颜 2024-12-22 02:50:57

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

assigns[:user]

(RSpec 文档< /a>)


编辑

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

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

Try using assigns with brackets instead of parenthesis.

assigns[:user]

(RSpec docs)


EDIT

One pattern I always use when testing controller actions is to ensure that a variable required by the associated view(s) is assigned. For example,

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
深海不蓝 2024-12-22 02:50:57

你有一个错字

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

我知道它适用于之前的测试用例...但仍然值得在这里至少进行一次广泛检查。当你确定时,你可以删除所有这些废话。

You've got a typo

response.should redirect_to(user_path(:user))

should be

response.should redirect_to(user_path(user))

Edit:
Try checking that the user is valid with:

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

I know it works on the previous test case... but still worth checking extensively at least once here. You can remove all that guff when you're certain.

作死小能手 2024-12-22 02:50:57

在您的测试中:

 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

In your test:

 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

Your @attr hash is different from @user. You are creating user with attributes of @attr, and asserting that it should redirect to @user show (which is different from one created). Change to:

 it "should redirect to the user show page" do
    post :create, :user => @attr
    user = assigns(:user)
    response.should redirect_to(user_path(user))
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文