Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby测试没有';t递增用户计数_Ruby On Rails_Ruby_Ruby On Rails 4_Devise_Functional Testing - Fatal编程技术网

Ruby on rails Ruby测试没有';t递增用户计数

Ruby on rails Ruby测试没有';t递增用户计数,ruby-on-rails,ruby,ruby-on-rails-4,devise,functional-testing,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,Functional Testing,我是Ruby新手,我正在尝试创建一个测试来创建一个新用户。我正在使用Desive,我的test_helper.rb看起来像: ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*

我是Ruby新手,我正在尝试创建一个测试来创建一个新用户。我正在使用Desive,我的test_helper.rb看起来像:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
   fixtures :all
# Add more helper methods to be used by all tests here...
end

class ActionController::TestCase
   include Devise::TestHelpers

  def sign_in_user
    @request.env["devise.mapping"] = Devise.mappings[:user]
    sign_in users(:user1)
  end
end
我的用户装置如下所示:

user1:
  id: 1
  email: user1@test.com
  encrypted_password: abcdef1
  role: Sales
  created_at: <%= Time.now %>
user1:
身份证号码:1
电邮:user1@test.com
加密密码:abcdef1
角色:销售
创建于:
我的测试非常简单:

require 'test_helper'
class UsersControllerTest < ActionController::TestCase
  setup do
    @controller = UsersController.new
    sign_in_user
  end

  test "should create user" do
    assert_difference('User.count') do
      post :create, user: {name: "Test", password: "Troll", password_confirmation: "Troll", email: "test@test.com", role: "Sales"}
    end

    assert_redirected_to user_path(assigns(:user))
  end
end
需要“测试助手”
类UsersControllerTest

但每当我运行rake测试时,我总是得到一个错误,User.count没有增加1(预期为2,但为1)。我做错了什么?任何帮助都将不胜感激

在控制器代码中-为什么不检查日志和/或添加一些puts语句,例如“puts params.inspect”和添加“puts@use.inspect”(在它尝试创建用户之后),以查看控制器的实际情况。由于输入不是预期的或其他原因,它可能无法通过一些验证…感谢您的快速响应!我是否只是将puts params.inspect添加到User.rb的def create部分?不幸的是,我没有为用户控制器编写代码(我只是在测试),所以我真的不明白发生了什么。以下是用户控制器的代码:'@User=User.new(User_params)。。。。如果是user.save。。。。flash[:success]=“用户创建”。。。。将_重定向到@user。。。。其他的呈现“新”。。。。注释中的结束代码格式很糟糕-编辑你的问题并在那里添加代码。。。是-在控制器中的
def create
方法中添加一些puts语句。。。您可以随时在以后删除它们,但现在在您运行测试时,它会吐出一些有用的数据。@VEL,您介意为app/models/user.rb发布您的全部源代码吗?您还可以提供您的db/schema.rb吗?