【发布时间】:2015-05-18 21:23:19
【问题描述】:
我在用户和组织之间有关系:
User migration file: t.references :organization, index: true, foreign_key: true
t.string :xml_file, null: false
User model file: belongs_to :organization
Organization model file: has_one :user
用户模型包含一个变量xml_file,它是一个存储在某处的文件(尚未编写该控制器方法)。我正在尝试编写集成测试:
def setup
emptymap = fixture_file_upload('test/fixtures/empty.xml', 'application/xml', :binary) # I have uploaded this file in the fixtures directory.
@organization = organizations(:one) # Which is in the organizations fixtures file.
@user = @organization.users.build(xml_file: emptymap)
end
test "should be valid" do
assert @user.valid?
end
这会产生错误(参考测试中的@user 行):
NoMethodError: undefined method `users' for #<Organization:0x00000008a41d18>
如果我将def setup 中的@user 行更改为@user = @organization.user.build(xml_file: emptymap),则会生成错误消息:NoMethodError: undefined methodbuild' for nil:NilClass`。所以好像也不是这样。
知道我在这里做错了什么吗?
【问题讨论】:
-
@organization.user.build不是users -
那么它仍然会产生错误信息:
NoMethodError: undefined method 'build' for nil:NilClass -
其实是
@organization.build_user
标签: ruby-on-rails ruby ruby-on-rails-4 integration-testing