ruby-on-rails - 如何检查 'kind_of?' 到 'count' 或在 RSpec 'its' 功能内运行其他方法?

标签 ruby-on-rails ruby ruby-on-rails-3 rspec rspec2

我正在使用 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/

相关文章:

ruby - Socket 是否与 Pry 捆绑在一起?

ruby-on-rails-3 - PG::Error: ERROR: 将记录插入关联模型时,列 "created_at"中的空值

ruby-on-rails - Rails 小写参数用于包含验证?

ruby-on-rails - 是否有 Rails 控制台命令(Rails 3+)来重新加载更改后的代码?

mysql - 如何获取未加倍的记录?

mysql - 使用 rake db :migrate command with mysql 在 Rails 中创建表的主键问题

ruby-on-rails - Node.js 和 Rails 应用程序托管,具有共享 MongoDB 数据库和定价

ruby-on-rails - Rails 防止多态 has_many 中的重复项 :through associations

ruby-on-rails - TinyTds::错误:无法将值 NULL 插入列 'ID'

ruby - 为什么这些字符串表达式打印的结果不同?