“用户数"没有改变 1 - Rails [英] "User.count" didn't change by 1 - Rails

查看:29
本文介绍了“用户数"没有改变 1 - Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"User.count" 没有改变 1 意味着什么以及如何修复它?下面是来自控制台的命令行.

以下是我从 bundle exec rake test 得到的失败,我正在寻找修复它们的方法:

$ bundle exec rake test运行选项:--seed 210# 跑步:……噗……噗……以 1.084264 秒、11.0674 次运行/秒、13.8343 次断言/秒完成.1) 失败:UsersControllerTest#test_should_create_user [app/test/controllers/users_controller_test.rb:20]:User.count"没有改变 1.预期:3实际:22) 失败:UsersControllerTest#test_should_update_user [app/test/controllers/users_controller_test.rb:39]:预期响应为<redirect>,但为<200>12 次运行,15 次断言,2 次失败,0 次错误,0 次跳过

另外,预期响应为 <重定向>,但为 <200>" 是什么意思?

下面的代码是我在这个测试中运行的:

下面是 users_controller_test.rb

中的第 39 行

 37 test "should update user" do38 补丁 :update, id: @user, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }39 assert_redirected_to user_path(assigns(:user))40 结束

这是控制器中的代码.对于 1),下面是 users_controller_test.rb

中的第 20 行

 19 test "should create user" do20 assert_difference('User.count') 做21 post :create, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }22 结束

下面是我的users_controller#update 的样子

def 更新response_to do |格式|如果@user.update(user_params)format.html { redirect_to @user,注意:'用户已成功更新.'}format.json { 渲染:显示,状态::ok,位置:@user }别的format.html { 渲染:编辑}format.json { 渲染 json: @user.errors, 状态: :unprocessable_entity }结尾结尾结尾

前提条件:

我后来添加了有价值的电子邮件 我按照 Jonathan MacDonald 的 YouTube 教程如何在 Windows 7 上安装 Ruby on Rails - 第 4 部分,Ruby on Rails"创建了我的初始用户模型11:00 开始处理

$ rails 生成脚手架用户 first_name:string last_name:string$耙数据库:迁移

然后在控制台的沙箱中玩了一会儿之后,我通过以下过程在我的用户模型中添加了电子邮件值:6.3.1 A hashed password from railstutorial org's e-book:>

原始命令:

$ rails 生成迁移 add_password_digest_to_users password_digest:string我在用户模型中添加电子邮件值的命令:$ rails 生成迁移 add_email_to_users email:string$耙分贝:迁移

然后我更新了以下文件:

控制器

1) 'myapp'/app/controllers/users_controller.rb[在permit()中添加:email]

def user_paramsparams.require(:user).permit(:first_name, :last_name, :email)结尾

2) 'myapp/test/controllers/users_controller_test.rb'[添加电子邮件:@user.email 在'test "should create user" do' 和'test "should update user" do']

test "should create user" doassert_difference('User.count') 做post :create, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }结尾# ...测试应该更新用户"做补丁:更新,id:@user,用户:{first_name:@user.first_name,last_name:@user.last_name,电子邮件:@user.email,}assert_redirected_to user_path(assigns(:user))结尾

模型

1) 'myapp'/app/models/user.rb[通过添加这一行为电子邮件添加验证]

validates :email, Presence: true, length: { maximum: 255 }

2) 'myapp'/test/modles/user_test.rb[在用户设置的定义中添加电子邮件值]

def 设置@user = User.new(first_name: "Example", last_name:"User", email: "aaaa@bbbbbbb.com")结尾[添加电子邮件验证任务]测试电子邮件应该存在"做@user.email = " "assert_not @user.valid?结尾测试电子邮件不应太长"做@user.email = "a" * 244 + "@example.com"assert_not @user.valid?结尾

观看次数

1) 'myapp'/app/views/users/_form.html.erb[添加电子邮件字段]

<%= f.label :email %><br><%= f.text_field :email %>

2) 'myapp'/app/views/users/index.html.erb[在表格中添加电子邮件地址]

<%=用户.email %></td>

3) 'myapp'/app/views/users/show.html.erb[在段落中添加电子邮件部分]

<strong>电子邮件:</strong><%=@user.email %></p>

然后当我运行 bundle exec rake test 时,我遇到了我问的这些错误.

有一个名为 users.yml 的文件,一开始我不认为这是相关的.但它实际上是由这些故障引起的.所以我在回答中引用了这个文件中的代码.为简单起见,我没有在我的问题部分提及它.

解决方案

这些失败是由于 'myapp/test/fixtures/users.yml' 中缺少 email 属性而导致的后来在生产环境中添加了.我使用 'first_name''last_name' 创建了初始用户模型,而没有 'email',正如我在问题中的步骤中所述.

有很多命令可以帮助在相关文件中为初始创建后新添加的属性添加属性.但是我已经在相关文件中手动添加了属性,如下面的代码所示.

'myapp/test/fixtures/users.yml'

原文:

一:名字:MyString姓氏:MyString二:名字:MyString姓氏:MyString

更新:

一:名字:MyString姓氏:MyString电子邮件:MyString二:名字:MyString姓氏:MyString电子邮件:MyString

然后运行

$ bundle exec rake 测试运行选项:--seed 1540# 跑步:……………………在 2.383035 秒内完成,6.2945 次运行/秒,8.8123 次断言/秒.15 次运行,21 次断言,0 次失败,0 次错误,0 次跳过


但是,我已经在下面的列表中总结了1"和200"的含义.

[1"是什么意思]
要了解这个数字 '1' 的含义,您需要阅读接下来的几行

预期:3实际:2

它说预期是 3,但实际上是 2.在 'myapp/test/fixtures/users 中只有 2 个属性 'first_name''last_name'.yml' 在乞讨时它假设有 3 个属性,分别是 'first_name''last_name''email'然后我猜测这条消息提到了 'myapp/test/fixtures/users.yml' 中缺少的属性数量,通过提取数字 '1' ..

[回答200"]
我认为这个数字也有独特的含义,即相关的源代码而不是 HTTP 状态代码.它实际上只是 HTTP 状态代码.代码 '200' 表示正常,如下面的链接所示.

<块引用>

10.2.1 200 正常

请求成功.响应返回的信息取决于请求中使用的方法...

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

它表示预期的响应是重定向而不是彻底.

What "User.count" didn't change by 1 means and how to fix it? Below is command line from console.

Here are the failures that I've got from bundle exec rake test and I'm looking for a way to fix them:

$ bundle exec rake test
 Run options: --seed 210

 # Running:

 .....F...F..

 Finished in 1.084264s, 11.0674 runs/s, 13.8343 assertions/s.

   1) Failure:
 UsersControllerTest#test_should_create_user  [app/test/control
 lers/users_controller_test.rb:20]:
 "User.count" didn't change by 1.
 Expected: 3
   Actual: 2


   2) Failure:
 UsersControllerTest#test_should_update_user [app/test/control
 lers/users_controller_test.rb:39]:
 Expected response to be a <redirect>, but was <200>

 12 runs, 15 assertions, 2 failures, 0 errors, 0 skips

And addition, what does "Expected response to be a <redirect>, but was <200>" mean?

And code below is what I've run in this test:

Below is line 39 in users_controller_test.rb

  37   test "should update user" do
  38     patch :update, id: @user, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }
  39     assert_redirected_to user_path(assigns(:user))
  40   end

And here is code in controller. For 1), below is line 20 in users_controller_test.rb

  19   test "should create user" do
  20     assert_difference('User.count') do
  21       post :create, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }
  22     end

Below is what my users_controller#update looks like

def update
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render :edit }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

Pre Conditions:

I've added email valuable later I've created my initial user model by following Jonathan MacDonald's YouTube tutorial "How to install Ruby on Rails on Windows 7 - Part 4, Ruby on Rails" Process starting at 11:00

$ rails generate scaffold user first_name:string last_name:string
$ rake db:migrate

And then after playing around in console's sandbox a bit, I've added email value in my user model by following process in 6.3.1 A hashed password from railstutorial org's e-book:

Original command:

$ rails generate migration add_password_digest_to_users password_digest:string
My command to add email value in my user model:
$ rails generate migration add_email_to_users email:string
$ rake db:migarate

and then I've update following files:

Controllers

1) 'myapp'/app/controllers/users_controller.rb [add :email in permit()]

def user_params
  params.require(:user).permit(:first_name, :last_name, :email)
end

2) 'myapp/test/controllers/users_controller_test.rb' [add email: @user.email in 'test "should create user" do' and 'test "should update user" do']

test "should create user" do
    assert_difference('User.count') do
    post :create, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }
end
# ...
test "should update user" do
    patch :update, id: @user, user: { first_name: @user.first_name, last_name: @user.last_name, email: @user.email, }
    assert_redirected_to user_path(assigns(:user))
end

Models

1) 'myapp'/app/models/user.rb [add validaates for email by add this line]

validates :email, presence: true, length: { maximum: 255 }

2) 'myapp'/test/modles/user_test.rb [add email value in definition of user setup]

def setup
    @user = User.new(first_name: "Example", last_name:"User", email: "aaaa@bbbbbbb.com")
end

 [add email validation tasks]

    test "email should be present" do
        @user.email = "     "
        assert_not @user.valid?
    end
    test "email should not be too long" do
        @user.email = "a" * 244 + "@example.com"
        assert_not @user.valid?
    end

Views

1) 'myapp'/app/views/users/_form.html.erb [add field for email]

<div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
</div>

2) 'myapp'/app/views/users/index.html.erb [add email coulmn in table]

<td>
    <%= user.email %>
</td>

3) 'myapp'/app/views/users/show.html.erb [add email section in paragraphs]

<p>
    <strong>email:</strong>
    <%= @user.email %>
</p>

And then when I running bundle exec rake test, I've been facing with these error that I asked.

There is a file called users.yml and I didn't think this is relevant at the beginning. But it was actually been caused for these failures. So I refer code in this file in my answer. I do not refer it here in my question section for sake of simplicity.

解决方案

These failures have been caused by the email attribute missing in 'myapp/test/fixtures/users.yml' while the email attribute was added in the production environment later. I created the initial user model with 'first_name' and 'last_name' without 'email', as I described in the steps in my question.

There are many commands that help to add attributes in related files for newly added attribute after its initial creation. But I've added attributes in related files manually as you can see in below code.

'myapp/test/fixtures/users.yml'

Original:

one:
  first_name: MyString
  last_name: MyString
two:
  first_name: MyString
  last_name: MyString

Update:

one:
  first_name: MyString
  last_name: MyString
  email: MyString
two:
  first_name: MyString
  last_name: MyString
  email: MyString

Then run

$ bundle exec rake test
  Run options: --seed 1540

  # Running:

  ...............

  Finished in 2.383035s, 6.2945 runs/s, 8.8123 assertions/s.

  15 runs, 21 assertions, 0 failures, 0 errors, 0 skips


However, I've summarized meanings of '1' and '200' in list down below.

[What dose '1' meant]
To understand what this number '1' meant, you need to read next couple lines

Expected: 3
  Actual: 2

It says expected 3 but actually 2. There is only 2 attribute 'first_name' and 'last_name' in 'myapp/test/fixtures/users.yml' at the begging while it suppose to have 3 attribute which is 'first_name', 'last_name' and 'email' then I'm guessing this message mentioned about number of missing attribute in 'myapp/test/fixtures/users.yml' by pulling up number '1'..

[Answer for '200']
I thought this number has also has unique meanings that related source code instead of HTTP status code. It actually just HTTP status code. The code '200' means just OK as you can see in link down below.

10.2.1 200 OK

The request has succeeded. The information returned with the response is dependent on the method used in the request...

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

It says expected response to be redirect instead of just went thorough.

这篇关于“用户数"没有改变 1 - Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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