我已经构建了一个基于Hartl课程的应用程序 - 直到并包括第9.2节(认证)。从那里我试图通过添加组织模型来扩展功能。最初,我尝试使用脚手架生成命令行功能,决定它已经建立了太多,将其翻转并手动构建。我相信我可能搞砸了我的迁移(见下文)。我使用Rails v4.1.1,Rspec 3.0.0和Capybara 2.2.0 - 所有数据库都是postgres。Rspec在尝试测试嵌套属性时抛出的未知属性错误
一个组织可以拥有多个用户,而每个用户只属于一个组织。
我应该注意到,在我自己的机器上使用开发环境,我有组织在用户注册过程中正常工作的领域 - 它创建一个新用户,我可以正确查看组织和用户页面,并且有应用程序的性能没有出现任何错误。我的问题,当我使用Rspec的测试测试开始 - 抛出了一系列的错误是这样的:
ActiveModel::MissingAttributeError:
can't write unknown attribute 'organization_id'
我的模型:
class Organization < ActiveRecord::Base
validates :organization_name, presence: true, length: { maximum: 50 }
has_many :users
end
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
before_save { self.email = email.downcase }
before_create :create_remember_token, :create_organization
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
has_secure_password
validates :password, length: {minimum: 6}
def create_organization
self.organization = Organization.create(:organization_name => Organization.organization_name) unless self.organization.present?
end
我的工厂:
FactoryGirl.define do
factory :organization do
organization_name "Orgname"
end
factory :user do
association :organization
name "A Name"
email "[email protected]"
password "foobar"
password_confirmation "foobar"
end
end
一个例子Rspec的测试套件:
describe "edit" do
let(:organization) { FactoryGirl.create(:organization) }
let(:login) { FactoryGirl.create(:login,
:organization => organization,
:user => FactoryGirl.create(:user)) }
before do
sign_in login
visit edit_user_path (user)
end
我schema.rb文件:
ActiveRecord::Schema.define(version: 20140619041326) do
create_table "organizations", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "organization_name"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.integer "organization_id"
t.string "remember_token"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["organization_id"], name: "index_users_on_organization_id", using: :btree
add_index "users", ["remember_token"], name: "index_users_on_remember_token", using: :btree
end
这可能是值得一提的是在这里当我第一次开始运行我使用哈特尔的书推荐Rspec的版本测试 - 这意味着我必须通过从开发DB的变化给力使用bin/rake dg:migrate RAILS_ENV = test命令行函数来测试数据库。每次我跑了这一点,我schema.rb文件(如上面贴)就失去了行
t.integer "organization_id"
,我不得不耙分贝得到它的再次合作。
我意识到这里有很多信息,而且我的问题可能很愚蠢(我对语言和框架很陌生,这是我的第一个应用程序),但它让我生气。过去一周,我花了很多时间试图找出当实际的应用程序本身工作时为什么测试会被破坏。
任何帮助将不胜感激。
我已经找到了问题 - 显然轨道4不会自动更新spec_helper使用'的ActiveRecord :: Migration.maintain_test_schema'的模式 - 而不是你仍然需要使用“rake db:test:prepare”。 不过虽然这已摆脱了我一些错误,我仍然得到3个主要错误: 1)它不喜欢我厂“登陆”(我是复制这似乎暗示这会工作其他代码) 2.)在用户页面rspec测试它找不到字段“机构名称” 3.)它说上述“create_organization”功能未定义方法“organization_name” 任何想法? – Zoinks10