我正在使用 Ruby on Rails 3.0.9、RSpec-rails 2 和 FactoryGirl。我正在尝试使用 RSpec its
功能检查数组中存在的 kind_of?
、count
、... 项。也就是说,我想做如下的事情:
subject do
Factory(
:user,
:articles => 3.times.inject([]) { |articles,i|
articles << Factory(
:article,
:user_id => Factory(:user)
)
}
end
# Note the (wrong!) usage of 'first', 'count' methods
its(:articles) { first.should be_kind_of(Article) }
its(:articles) { count.should == 10 }
但是,由于我收到错误,似乎不可能以我在上面的代码中实现的(错误!)方式实现它。 有没有办法做到这一点(检查kind_of?
,计数
,...)?
最佳答案
http://rdoc.info/github/rspec/rspec-core/master/RSpec/Core/Subject/ClassMethods:its :
describe Person do
subject do
Person.new.tap do |person|
person.phone_numbers << "555-1212"
end
end
its("phone_numbers.first") { should eq("555-1212") }
end
所以在你的情况下:
its('articles.first') { should be_kind_of(Article) }
its('articles.count') { should == 10 }
关于ruby-on-rails - 如何检查 'kind_of?' 到 'count' 或在 RSpec 'its' 功能内运行其他方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7489044/