如何测试我的表设计是否正确实现了关联?
我已经编写了基本模型并定义了它们的关联以及创建关联表的迁移。
编辑 - 强调我特别想要测试的内容。
我希望能够测试:
- 关联按预期配置
- 表结构正确支持关联
我已经为所有模型编写了 FG 工厂,期望拥有一组完整的测试数据,但我无法掌握如何编写规范来测试 own_to 和 has_many 关联。
例如,给定一个 has_many 用户的组织,我希望能够测试我的示例组织是否具有对我的示例用户的引用。
Organization_Factory.rb:
Factory.define :boardofrec, :class => 'Organization' do |o|
o.name 'Board of Recreation'
o.address '115 Main Street'
o.city 'Smallville'
o.state 'New Jersey'
o.zip '01929'
end
Factory.define :boardofrec_with_users, :parent => :boardofrec do |o|
o.after_create do |org|
org.users = [Factory.create(:johnny, :organization => org)]
end
end
User_Factory.rb:
Factory.define :johnny, :class => 'User' do |u|
u.name 'Johnny B. Badd'
u.email '[email protected]'
u.password 'password'
u.org_admin true
u.site_admin false
u.association :organization, :factory => :boardofrec
end
Organization_spec.rb:
...
it "should have the user Johnny B. Badd" do
boardofrec_with_users = Factory.create(:boardofrec_with_users)
boardofrec_with_users.users.should include(Factory.create(:johnny))
end
...
此示例失败,因为 Organization.users 列表和比较 User :johnny 是同一 Factory 的不同实例。
我意识到这并不遵循这些插件(FG、rspec)似乎适合的 BDD 思想,但鉴于这是我的第一个 Rails 应用程序,我在不知道我已经配置了关联和表的情况下继续前进感到不舒服结构正确。
I have written my basic models and defined their associations as well as the migrations to create the associated tables.
EDIT - Adding emphasis to what I specifically want to test.
I want to be able to test:
- The associations are configured as intended
- The table structures support the associations properly
I've written FG factories for all of my models in anticipation of having a complete set of test data but I can't grasp how to write a spec to test both belongs_to and has_many associations.
For example, given an Organization that has_many Users I want to be able to test that my sample Organization has a reference to my sample User.
Organization_Factory.rb:
Factory.define :boardofrec, :class => 'Organization' do |o|
o.name 'Board of Recreation'
o.address '115 Main Street'
o.city 'Smallville'
o.state 'New Jersey'
o.zip '01929'
end
Factory.define :boardofrec_with_users, :parent => :boardofrec do |o|
o.after_create do |org|
org.users = [Factory.create(:johnny, :organization => org)]
end
end
User_Factory.rb:
Factory.define :johnny, :class => 'User' do |u|
u.name 'Johnny B. Badd'
u.email '[email protected]'
u.password 'password'
u.org_admin true
u.site_admin false
u.association :organization, :factory => :boardofrec
end
Organization_spec.rb:
...
it "should have the user Johnny B. Badd" do
boardofrec_with_users = Factory.create(:boardofrec_with_users)
boardofrec_with_users.users.should include(Factory.create(:johnny))
end
...
This example fails because the Organization.users list and the comparison User :johnny are separate instances of the same Factory.
I realize this doesn't follow the BDD ideas behind what these plugins (FG, rspec) seemed to be geared for but seeing as this is my first rails application I'm uncomfortable moving forward without knowing that I've configured my associations and table structures properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的用户工厂已经通过 Factory Girl
association
方法创建了一个组织:运行规范后查看“log/test.log”——您应该看到组织和组织的 INSERT用户。
如果您想在没有 Factory Girl 关联的情况下进行测试,请创建一个仅创建用户的工厂并在规范中进行关联:
当然,这一切都是为了测试 ActiveRecord 是否正在执行其工作。由于 ActiveRecord 已经经过了彻底的测试,一旦您确信该框架确实完成了它应该做的事情,您就需要集中精力测试应用程序的功能。
Your user factory already creates an organization by virtue of the Factory Girl
association
method:Take a look at 'log/test.log' after running your spec -- you should see an INSERT for both the organization and the user.
If you wanted to test this without the Factory Girl association, make a factory that just creates the user and make the association in the spec:
Of course all this is doing is testing whether ActiveRecord is doing its job. Since ActiveRecord is already thoroughly tested, you'll want to concentrate on testing the functionality of your application, once you've convinced yourself that the framework actually does what it's supposed to do.