Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 检查user.u是否有效时RSpec测试失败_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails 检查user.u是否有效时RSpec测试失败

Ruby on rails 检查user.u是否有效时RSpec测试失败,ruby-on-rails,rspec,Ruby On Rails,Rspec,我是测试新手,不知道为什么会失败: 我有个测试 require 'spec_helper' require 'cancan/matchers' describe User do subject(:user) { User.create!(email: 'test@test.com', password: '12345678', password_confirmation: '12345678', goal_id: '1', experience_level_id: '1', gender:

我是测试新手,不知道为什么会失败:

我有个测试

require 'spec_helper'
require 'cancan/matchers'

describe User do
  subject(:user) { User.create!(email: 'test@test.com', password: '12345678', password_confirmation: '12345678', goal_id: '1', experience_level_id: '1', gender: 'Female') }

it "should be valid with a name, goal, password, password_confirmation, experience_level, and gender" do
  user.should be_valid
end
我认为这应该通过。我可以通过站点前端创建用户,但我收到以下失败消息:

1) User should be valid with a name, goal, password, password_confirmation, experience_level, and gender
     Failure/Error: subject(:user) { User.create!(email: 'test@test.com', password: '12345678', password_confirmation: '12345678', goal_id: '1', experience_level_id: '1', gender: 'Female') }
     RuntimeError:
       Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
     # ./app/models/user.rb:32:in `add_initial_program'
     # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:13:in `block (2 levels) in <top (required)>'

prog\u id=Program.for\u user(self)。first.id
表示“获取与此用户关联的第一个程序的id”。根据我们所看到的,我们可以推断
Program.for_user(self)。首先
是nil,并且对该nil调用
id
方法。您确定测试数据库中保存了与您在测试中创建的用户关联的程序模型吗?您似乎并没有在任何地方创建此模型,而且,根据您的代码,在您尝试创建用户之前,有一个相关的程序是至关重要的

编辑:

您可以在主题块中初始化程序,如下所示:

subject(:user) do 
    Program.create!(#whatever attributes are appropriate)
    User.create!(email: 'test@test.com', password: '12345678', password_confirmation: '12345678', goal_id: '1', experience_level_id: '1', gender: 'Female')
end

块中的最后一个值是subject块的相关返回值,因此只要块返回用户模型,即使块中有多条语句,一切都应该正常。对我来说,这是您可以单独更改测试的最干净的初始化选项,因为很明显,用户和程序紧密地绑定在一起,因此要拥有一个有效的用户,您首先必须拥有一个关联的程序。我确实认为,如果用户模型中不存在创建程序的逻辑(您可能想检查查询方法),那么在用户模型中创建一个程序会更好,但是块中的多个语句现在应该可以工作了。

app/models/User.rb:32
是罪魁祸首,至少罪魁祸首是在这里表现出来的。如果你需要更多帮助,请分享你的模型。好的,我添加了用户模型的相关部分。我可以通过网站创建一个用户,所以我不明白为什么规范会因为用户模型的问题而失败。可能是因为测试数据库中不存在程序,所以它是零?这并不能回答您的问题,但我建议您不要在测试中硬编码ID。我会用FactoryGirl之类的东西来代替。你说得对!我在测试中还没有一个程序模型。我将如何创建它,只需为程序添加另一个descripe块?我将把它包括在
subject
方法中-subject方法返回的值很重要,但您可以在块中做更多的事情。例如,您可以执行`subject{Program.create!(…);User.create!(…)},并且只要您的程序与您的用户有适当的链接,就可以了。我将编辑答案,以比评论中更清楚地显示这一点。太棒了!谢谢我一直在想如何做到这一点。同时,感谢你教我如何在主题中添加多个内容。这是一个巨大的帮助。
def self.for_user(user)
  where(:goal_id => user.goal_id, :experience_id => user.experience_level_id, :gender => user.gender)   
end
subject(:user) do 
    Program.create!(#whatever attributes are appropriate)
    User.create!(email: 'test@test.com', password: '12345678', password_confirmation: '12345678', goal_id: '1', experience_level_id: '1', gender: 'Female')
end