2014-06-09 34 views
0

是否有可能通過主體屬性,像這樣循環:如何遍歷RSpec中的主題屬性?

subject { assignment } 

its(:submissions).each do |submission| 
    submission.grade should eq 100 
end 

我得到的錯誤是Undefined method each for Class ...

我也試過

subject { assignment } 
its(:submissions) do 
    each do |submission| 
    submission.grade should eq 100 
    end 
end 

,並得到

未定義的方法'each'for RSpec :: Core :: ExampleGroup :: Nested_4 :: Nested_4 :: Nested_1 :: Nested_1:0x00000005f91ac8

回答

1

您可以通過subject方法獲得主題。所以,你可以做你想做的是這樣的:

subject { assignment } 
it "has some number of submissions, each with a grade of 100" do 
    subject.submissions.each { |submission| submission.grade.should eq 100 } 
end 

旁註:Explicitly using subject makes your tests harder to read.相反,使用let

let(:assignment) { create :assignment } # or whatever is necessary to create the assignment 
it "has some number of submissions, each with a grade of 100" do 
    assignment.submissions.each { |submission| submission.grade.should eq 100 } 
end