预期响应为< redirect&gt ;,但为< 200>. [英] Expected response to be a <redirect>, but was <200>

查看:85
本文介绍了预期响应为< redirect&gt ;,但为< 200>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用配置文件创建用户的UsersController中测试create动作;然后将用户重定向到用户的个人资料页面,但出现错误Expected response to be a <redirect>, but was <200>.

I'm trying to testing the create action in the UsersController where the user is created with a profile; then the user is redirected to the user's profile page, but i get the error Expected response to be a <redirect>, but was <200>.

在用户控制器中创建操作

CREATE ACTION IN USER CONTROLLER

def create
  @user = User.new(user_params)
  if @user.save
    log_in @user
    flash[:success] = "Welcome to the Mini Olympics"
    redirect_to user_profile_path(current_user, @profile)
  else
    render 'new'
  end
end

使用有效信息进行注册测试

SIGNUP TEST USING VALID INFORMATION

test "valid signup information" do
  get signup_path
  assert_difference 'User.count', 1 do
    post users_path, user: @user.attributes
    @user.build_profile(name: "Micheal Example", street: "24 Martins St.", city: "Waterloo", state: "AW", zipcode: "22456")
    @user.save
    #assert_redirected_to user_profile_path(@user)
    assert_redirected_to @user.profile
  end
  assert_template 'profiles/show'
  assert is_logged_in?
end

推荐答案

该错误消息表示create操作失败,它呈现为渲染而不是重定向到用户的配置文件(状态 302 )再次输入表单(状态 200 ).

The error message means that the create action is failing, and instead of redirecting to the user's profile (status 302), it renders the form again (status 200).

关于为什么失败,目前尚不清楚.该测试似乎混淆了 controller 中的@user值和 test 中的@user值,这是两个完全不同的对象.

As to why it fails, that is unclear. The test appears to confuse the value of @user in the controller, with the value of @user in the test, which are two completely different objects.

在测试动作时,您将值通过params哈希传递,就像您在上一行中所做的一样:

When testing an action, you pass the values in through the params hash, as you do above in the line:

post users_path, user: @user.attributes

但是,由于没有充分的理由,测试随后再次设置了@user的值.我不清楚@user的原始值( test 中的那个)在哪里获得其属性.

But the test then sets the value of @user again, for no good reason. It's not clear to me where the original value of @user (the one in the test) gets its attributes.

您可能想要执行以下操作:

You probably want to do something like this:

 assert_difference 'User.count', 1 do
   user = User.new
   user.build_profile(name: "Micheal Example", street: "24 Martins St.", city: "Waterloo", state: "AW", zipcode: "22456")
   post users_path, user: user.attributes
   assert_redirected_to user.profile
 end

局部变量user只是为生成属性哈希值的一种方便.您可以轻松地将属性直接分配给哈希,然后将其传递给post.

The local variable user is just a convenience for generating the values for the attributes hash. You could just as easily assign the attributes directly to a hash and pass them to post.

这篇关于预期响应为&lt; redirect&gt ;,但为&lt; 200&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆