2010-12-10 63 views
2

我是新的Ruby和Rspec。我正在寫我的第一個RSpec測試,我認爲我的代碼不太好。但我不知道如何讓它變得更好。DRS問題與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 

回答

1

所以這裏的我有時會做這種事情的方式:

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 
+1

我會用本發明的方法對於這一點,就像它在這裏完成:https://github.com/rspec/rspec-expectations/blob/master/features/expectations/attribute_of_subject.feature – iain 2010-12-10 23:43:44

+0

謝謝您爲您的幫助 – ThreeFingerMark 2010-12-13 22:37:19

1

記住,你的測試並不需要是真幹。不要爲此犧牲可讀性。

將您的規格視爲例子:哪些示例應該有效,哪些不應該?這也是測試正則表達式的線索:提供一些例子,通過和一些不。

對於驗證,我做了一些自定義匹配器,可以找到here。例如:

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 
+0

感謝您的幫助 – ThreeFingerMark 2010-12-13 22:29:28