2014-09-28 63 views
0

希望這是一個簡單的問題...使用設計時,我應該在我的Rails應用程序的模型中測試什麼?

我看到設計看起來經過很好的測試,但我寧願安全而不抱歉。在模型規範中需要測試的最低項目是否已經由設計自己的測試覆蓋?

使用ruby 2.1.2,rails 4.1.6,rspec-rails 3.1.0,& devise 3.3.0。目前我的模型規範的樣子:

describe User do 
    before(:each) { @user = create(:user) } 

    subject { @user } 

    describe "factory" do 
    it { should be_valid } 
    end 

    describe "class instance" do 
    it { should respond_to(:email) } 
    it { should respond_to(:encrypted_password) } 
    it { should respond_to(:reset_password_token) } 
    it { should respond_to(:reset_password_sent_at) } 
    it { should respond_to(:remember_created_at) } 
    it { should respond_to(:sign_in_count) } 
    it { should respond_to(:current_sign_in_at) } 
    it { should respond_to(:last_sign_in_at) } 
    it { should respond_to(:current_sign_in_ip) } 
    it { should respond_to(:last_sign_in_ip) } 
    it { should respond_to(:created_at) } 
    it { should respond_to(:updated_at) } 
    it { should respond_to(:name) } 
    end 

    describe "name" do 
    context "when it's not present" do 
     before { @user.name = " " } 
     it { should_not be_valid } 
    end 

    context "when it's too long" do 
     before { @user.name = "a" * 51 } 
     it { should_not be_valid } 
    end 

    context "when it's long enough" do 
     before { @user.name = "a" * 50 } 
     it { should be_valid } 
    end 
    end 

    describe "email" do 
    context "when it's not present" do 
     before { @user.email = " " } 
     it { should_not be_valid } 
    end 

    context "when it's format is invalid" do 
     it "should not be valid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] [email protected]] 
     addresses.each do |invalid_address| 
      @user.email = invalid_address 
      expect(@user).not_to be_valid 
     end 
     end 
    end 

    context "when it's format is valid" do 
     it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] [email protected]] 
     addresses.each do |valid_address| 
      @user.email = valid_address 
      expect(@user).to be_valid 
     end 
     end 
    end 

    context "when it's already taken" do 
     let(:new_user) { create(:user) } 

     before { @user = build(:user, email: new_user.email) } 
     it { should_not be_valid } 
    end 

    context "address with mixed case" do 
     let(:mixed_case_email) { "[email protected]" } 

     it "should be saved as all lower-case" do 
     @user.email = mixed_case_email 
     @user.save 
     expect(@user.email).to eq mixed_case_email.downcase 
     end 
    end 
    end 

    describe "password" do 
    context "when it's not present" do 
     before { @user.password = @user.password_confirmation = " " } 
     it { should_not be_valid } 
    end 

    context "when it doesn't match the password confirmation" do 
     before { @user.password_confirmation = "mismatch" } 
     it { should_not be_valid } 
    end 

    context "when it's too short" do 
     before { @user.password = @user.password_confirmation = "a" * (Rails.application.config.devise.password_length.min - 1) } 
     it { should_not be_valid } 
    end 
    end 
end 

之類的東西,包括factory_girl方法,實際出廠配置,包括導軌/ spec_helper和其他安裝/省略的配置選項...讓我知道我是否應該包括這些或任何其他項目,以幫助澄清。

回答

2

我通過第三方代碼來實現測試功能的做法可能會包括三道防線:

  1. 測試模型的有效性進行任何操作之前。你已經在你的describe "factory"規範中完成了。
  2. 測試模型驗證是否按預期工作。這往往感覺像是過度殺傷,但它對迴歸測試很有價值,而且偶爾會發現過寬或過窄的驗證錯誤。無論如何,你也做到了這一點。
  3. 寫入驗收測試,顯示關鍵的應用程序級別的交互,如登錄,註銷,重置密碼等正在按預期工作。爲此,你需要使用水豚或類似的東西。
+0

我猜測驗收測試應該在功能規格中完成,否? – 2014-09-29 00:27:24

+0

是的,確切地說。它可以是一個標準的Rails集成測試,也可以使用Capybara或其他一些類似的東西。任何以與瀏覽器中的最終用戶相同的方式來執行整個應用程序的任何東西。 – 2014-09-29 08:06:21

相關問題