ruby-on-rails - 如何干掉 Rails 的 RSpec 测试

标签 ruby-on-rails ruby rspec

我正在学习 RSpec,但我注意到我的代码中有很多重复项。以下只是许多其他例子中的两个例子。有没有一种方法可以创建共享测试而不必遍历每个单独的属性?

  describe "validation" do
    describe "user_id" do
      it "should not be blank or nil" do
        @comment.user_id = nil
        @comment.should_not be_valid
        @comment.user_id = " "
        @comment.should_not be_valid
      end

      it "should an integer" do
        @comment.user_id = "a"
        @comment.should_not be_valid        
      end
    end

    describe "post_id" do
      it "should not be blank or nil" do
        @comment.post_id = nil
        @comment.should_not be_valid
        @comment.post_id = " "
        @comment.should_not be_valid
      end

      it "should an integer" do
        @comment.post_id = "a"
        @comment.should_not be_valid        
      end      
    end
  end

最佳答案

你可以试试这个:https://github.com/thoughtbot/shoulda-matchers

它为您的模型提供了许多简单的匹配器

关于ruby-on-rails - 如何干掉 Rails 的 RSpec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15773874/

相关文章:

ruby-on-rails - Active Record 的 'joins' 方法背后是什么?

mysql - 根据关联的创建日期对关联对象进行排序

ruby-on-rails - 无法使用 xmpp4r_facebook 发送 Facebook 消息

ruby-on-rails - 应该是匹配器 "have_many"和 "order"的 Rails 4 语法

ruby-on-rails - 为什么初始化程序中定义的方法会间歇性地引发 0​​x104567910 错误,但可以通过重新启动服务器来解决?

ruby-on-rails - 在开发中禁用 Sprockets Assets 缓存

ruby-on-rails - rails : Relations (Select all answers from all topics)

ruby - Ruby 中的 private 和 private_class_method 关键字

ruby-on-rails - Rails 如何在下午 4 点(在不同区域)提前 2 天设置日期和时间(Rails,Rspec)

ruby-on-rails - 使用 spork 的 Rails 项目 - 总是必须使用 spork?