- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我是 Rails 和测试模型的新手。我的模型类是这样的:
class Tester < Person
has_one :company
accepts_nested_attributes_for :skill
end
而且我想在不使用任何其他 gem 的情况下使用 rspec 对“accepts_nested_attributes_for :skill”进行测试。我怎样才能做到这一点?
最佳答案
有方便的 shoulda
gem 匹配器用于测试 accepts_nested_attributes_for
,但您提到您不想使用其他 gem。因此,仅使用 Rspec,想法是设置包含所需 Tester
属性的 attributes
散列和包含所需 skill_attributes
的嵌套散列 技能
属性;然后传入Tester
的create
方法,看是否改变了Testers
的数量和Skills
的数量.类似的东西:
class Tester < Person
has_one :company
accepts_nested_attributes_for :skill
# lets say tester only has name required;
# don't forget to add :skill to attr_accessible
attr_accessible :name, :skill
.......................
end
你的测试:
# spec/models/tester_spec.rb
......
describe "creating Tester with valid attributes and nested Skill attributes" do
before(:each) do
# let's say skill has languages and experience attributes required
# you can also get attributes differently, e.g. factory
@attrs = {name: "Tester Testov", skill_attributes: {languages: "Ruby, Python", experience: "3 years"}}
end
it "should change the number of Testers by 1" do
lambda do
Tester.create(@attrs)
end.should change(Tester, :count).by(1)
end
it "should change the number of Skills by 1" do
lambda do
Tester.create(@attrs)
end.should change(Skills, :count).by(1)
end
end
哈希语法可能不同。此外,如果您有任何唯一性验证,请确保在每次测试之前动态生成 @attrs
哈希。干杯,伙计。
关于ruby - 使用 Rspec 在单元测试中测试 "accepts_nested_attributes_for",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9193367/
我尝试在 rails4 中创建一对多连接。但是,虽然我没有收到错误消息,但未存储嵌套属性。 我究竟做错了什么? 站型 class Station
我有 2 个模型之间的关联,Business 和 Address。企业有注册地址的地方。我这样做了如下。 class Business "Address", :foreign_key => :bus
我正在尝试将 user_id 添加到由父 Controller 构建的嵌套属性,但它似乎没有达到预期的效果? 即。我有一个名为 Place.rb 的模型,它 accepts_nested_attrib
我有一对多的关系: class Programa :restrict accepts_nested_attributes_for :roles ... end class Role p
我正在尝试通过嵌套模型保存图像 **型号: Listing has_many:photos accepts_nested_attributes_for :photos, :allow_
我的模型如下所示: class Template false base.nested_attributes_keys = {} end alias_metho
我正在使用 Rails 2.3.8 并接受_nested_attributes_for。 我有一个简单的类别对象,它使用 awesome_nested_set 来允许嵌套类别。 对于每个类别,我想要一
我最近将我的应用程序从 v4.2.0 升级到了 v4.2.4。事实证明,嵌套属性的保存顺序已更改。有没有办法定义先保存哪个“accepts_nested_attributes_for”。我能够注意到变
给定某种事物: class Thing { //...other stuff... "custom_field_values_attributes"=>{ "0"=>{ "
我正在尝试使用 accepts_nested_attributes 来创建一个复杂的表单。基于Nested Attributes文档,examples等,我已经像这样设置了模型: 用户模型: requ
所以我对 Rails 还很陌生,所以我可能遗漏了一些非常直接的东西.. 我想做的是在创建 Band 时创建一个 Artist。 模型 乐队.rb class Band
我有一个模型 Post哪个belongs_to一Section .有两个不同的 Section子类,我使用 STI 为每个子类实现不同的行为。在Post我希望每个 Section 都有一个选项卡.该选
我有 class Profile has_many :favorite_books, :dependent => :destroy has_many :favorite_quotes, :de
我有以下模型的下表: 用户(id, role_id) has_many :entries 类别(id, category_name) has_many :entries 条目(id, category
我有以下三个模型(Rails 2.3.8) class Outbreak :destroy has_many :locations, :through => :inciden
我可能完全混淆了两者,但我看到表单可以使用基于嵌套路由的数组参数促进关联,例如: <%= form_for [@project, @task]... 或使用 fields_for如果是父类的助手 ac
我有 3 个模型和它们的关联 class User true end class Player :rolable end class Manager :rolable end 我开箱即用,从 r
我有一个 Post模型。我想让用户在创建/更新帖子时创建帖子评论 accepts_nested_attributes_for :comments .但是,我不想让用户通过嵌套属性更新评论。 有没有办法
我有一个模型主题和帖子。主题 has_many :posts。 在主题模型中,我也接受了 :posts_nested_attributes_for , 似乎在使用 Post 的一些参数更新 Topic
我试图让我的用户表单也允许用户同时通过 form_for 填写他们的公司资料。出于某种原因,它没有显示公司字段。这是我的 Controller 和布局代码。 class User true end
我是一名优秀的程序员,十分优秀!