ruby-on-rails - Rspec 的 DRY 问题我该如何解决

标签 ruby-on-rails ruby rspec dry

我是 Ruby 和 Rspec 的新手。我正在编写我的第一个 RSpec 测试,我认为我的代码不是很好。但我不知道如何让它变得更好。

在这个文件中,我将检查我的地址类。 first_name 和 last_name 相同,但我有两个大块。我该如何重构我的代码?什么是检查 RegExp 的好方法。

谢谢。

specify { Factory.build(:address).should be_valid }


  ### first_name ###

  it "should be invalid without an first_name" do
    Factory.build(:address, :first_name => nil).should_not be_valid
  end


  context "first_name" do

    it "should be invalid with more than 20 chars" do
      Factory.build(:address, :first_name => "#{'b'*21}").should_not be_valid
    end

    it "should be invalid with less than 3 chars" do
      Factory.build(:address, :first_name => "ll").should_not be_valid
    end

    it "should be valid with an valid first_name" do      
       valid_names.each do |name|
        Factory.build(:address, :first_name => name).should be_valid
       end
    end

    it "should be invalid with an invalid first_name" do
      invalid_names.each do |name|
        Factory.build(:address, :first_name => name).should_not be_valid
      end
    end    
  end


  ### last_name ###

  it "should be invalid without an last_name" do
    Factory.build(:address, :last_name => nil).should_not be_valid
  end

  context "last_name" do
    it "should be invalid with more than 20 chars" do
      Factory.build(:address, :last_name => "#{'b'*21}").should_not be_valid
    end

    it "should be invalid with less than 3 chars" do
      Factory.build(:address, :last_name => "ll").should_not be_valid
    end

    it "should be valid with an valid last_name" do      
       valid_names.each do |name|
        Factory.build(:address, :last_name => name).should be_valid
       end
    end

    it "should be invalid with an invalid last_name" do
      invalid_names.each do |name|
        Factory.build(:address, :last_name => name).should_not be_valid
      end
    end    
  end
 def valid_names    
    ["Kai","Ülück's","Schmeißtzs","Rald","Dr. Franzen","rolfes","Lars Michael","Öcück","Mark-Anthony"] 
  end

  def invalid_names    
    ["-#+*32","         ","a& &lkdf","_-_.l##df"," aaadsa","M€lzer"]
  end

最佳答案

所以我有时会这样做:

describe Address do
  describe "validations" do
    before do
      @address = Factory(:address)
    end
    describe "#first_name" do
      #prove that your factory is correct
      it "should be valid" do
        @address.should be_valid
      end
      it "should be less than 20 chars" do
        @address.name = "0" * 20
        @address.should_not be_valid
      end
      it "should be more than 3 chars" do
        @address.name = "000"
        @address.should_not be_valid
      end
    end
  end
end

关于ruby-on-rails - Rspec 的 DRY 问题我该如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4413836/

相关文章:

ruby-on-rails - 验证外键时 accepts_nested_attributes_for 出现问题

ruby - 为什么 with_progress 做 "Thread.new"?

ruby - 无法安装 therubyracer

ruby-on-rails - Rails - .htaccess 不强制用户使用 https

ruby-on-rails - Rails respond_with 没有输出剩余调用的状态

ruby-on-rails - VCR 正在返回 UnhandledHTTPRequestError

ruby-on-rails - 何时以及何时不 stub /模拟测试

ruby-on-rails - RSpec:带参数的 stub 链?

ruby-on-rails - 无法克服 Rails 设置中的错误 - `LoadError: cannot load such file -- active_storage/engine`

python - Python 的 itertools.product 在 Ruby 中的等价物是什么?