테스트를위한 shoulda
보석 계산기가 있지만 다른 보석을 사용하고 싶지는 않다고 말씀하셨습니다. 따라서 Rspec 만 사용하면 Tester
속성이 필요하고 Skill
속성을 포함하는 skill_attributes
이라는 중첩 해시를 포함하는 attributes
해시를 설정하는 것이 좋습니다. 그런 다음 create
방법으로 Tester
으로 전달하고 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
해시를 생성해야합니다. 건배, 친구.