Rspec 的 DRY 问题我该如何解决

发布于 2024-10-06 18:39:58 字数 2148 浏览 7 评论 0原文

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

在这个文件中,我将检查我的地址类。名字和姓氏是相同的,但我有两个大块。我如何重构我的代码?以及检查 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

i am new in Ruby and Rspec. I am writing my first RSpec test and i think that my code is not very well. But i don't know how i can make it better.

In this file i will check my Address class. The first_name and last_name are the same but i have two big blocks for it. How can i refactor my code? And what is a good way to check RegExp.

Thank you.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-10-13 18:39:58

所以这就是我有时做这种事情的方式:

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

so here's the way I sometimes do this kind of thing:

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
送君千里 2024-10-13 18:39:58

请记住,您的测试不需要非常干燥。不要为此牺牲可读性。

将您的规范视为示例:哪些示例应该有效,哪些示例无效?这也是测试正则表达式的线索:提供一些通过的示例和一些未通过的示例。

为了进行验证,我制作了一些自定义匹配器,可以在此处找到。例子:

describe Address do
  it { should deny(:last_name).to_be(nil, "", "1", "br", "a& &lkdf","_-_.l##df", "lzer") }
  it { should allow(:last_name).to_be("Kai","Ülück's","Schmeißtzs","Rald","Dr. Franzen","rolfes","Lars Michael","Öcück","Mark-Anthony") }
end

Remember that your tests don't need to be really DRY. Don't sacrifice readability for it.

Think of your specs as examples: which examples should be valid and which shouldn't? That's also the clue to testing regexes: provide some examples that pass and some that don't.

For validations, I made some custom matchers, which can be found here. Example:

describe Address do
  it { should deny(:last_name).to_be(nil, "", "1", "br", "a& &lkdf","_-_.l##df", "lzer") }
  it { should allow(:last_name).to_be("Kai","Ülück's","Schmeißtzs","Rald","Dr. Franzen","rolfes","Lars Michael","Öcück","Mark-Anthony") }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文