2016-11-23 53 views
0

我是Rspecs中的新成員,所以我寫下的代碼是正確的...我使用了簡單的Rspec。目前Rspec正確運行,但客戶需要做什麼修改?
如果有人可以看看我的代碼,並幫助我走出這將不勝感激....在rails中編寫rspecs的正確方法

require 'spec_helper' 
require 'rails_helper' 

describe User do 
describe "fails" do 
    it "to create without email" do 
     user = User.create(:first_name => "test", :last_name => "test",:email => "") 
     user.errors.messages[:email][0].should eq "can't be blank" 
    end 
end 

describe "fails" do 
    it "to create without data" do 
     user = User.create(:first_name => "", :last_name => "", :email => "", :status => "") 
     user.errors.messages[:email][0].should eq "can't be blank" 
    end 
end 

describe "fail" do 
    it "to create without a valid email" do 
     user = User.create(:first_name => "", :last_name => "", :email => "[email protected]", :status => "") 
     expect(user).to be_valid, lambda { "expected valid email" } 
    end 
end 

describe "pass" do 
    it "to supports 2 letter domain qualifiers" do 
     user = User.create(:first_name => "", :last_name => "", :email => "[email protected]", :status => "") 
     user.save! 
    end 
end 

describe "pass" do 
    it "to supports 4 letter domain qualifiers" do 
     user = User.create(:first_name => "", :last_name => "", :email => "[email protected]", :status => "") 
     user.save! 
    end 
end 
end 

describe "pass" do 
    it "to supports 4 and more letter domain qualifiers" do 
     user = User.create(:first_name => "", :last_name => "", :email => "[email protected]", :status => "") 
     user.save! 
    end 
end 

describe "pass" do 
    it "to without first name" do 
     user = User.new 
     user.first_name = "" 
     user.last_name = "Test" 
     user.status = "active" 
     user.email = "[email protected]" 
     user.save! 
    end 
end 

describe "pass" do 
    it "to without last name" do 
     user = User.new 
     user.first_name = "Test" 
     user.last_name = "" 
     user.status = "active" 
     user.email = "[email protected]" 
     user.save! 
    end 
end 

describe "pass" do 
    it "to without status" do 
     user = User.new 
     user.first_name = "Test" 
     user.last_name = "Test" 
     user.status = "" 
     user.email = "[email protected]" 
     user.save! 
    end 
end 

describe "pass" do 
    it "to create new record without first name, last name and status" do 
     user = User.create(:first_name => "", :last_name => "", :email => "[email protected]", :status => "") 
     user.save! 
    end 
end 

describe "pass" do 
    it "to create new record with all parameter" do 
     user = User.new 
     user.first_name = "Test" 
     user.last_name = "Test" 
     user.status = "active" 
     user.email = "[email protected]" 
     user.save! 
    end 
end 

describe "pass" do 
    it "to update record" do 
     user = User.find_by_id(1127) 
     user.first_name = "Test" 
     user.last_name = "Test" 
     user.status = "active" 
     user.email = "[email protected]" 
     user.save! 
    end 
end 
+0

哪裏都是你的測試,你可以寫

expect { User.new(...).save }.to change(User, :count).by(1) 

? :) – fabersky

+0

非常感謝你@fabersky ...我也是新的堆棧... :)謝謝你的正確編輯... –

回答

0
  1. 你不必同時需要spec_helperrails_helperrails_helper已經需要spec_helperspec_helper適用於不依賴於Rails的規範(例如lib目錄中的類)。 rails_helper適用於依賴於Rails的規格(例如您的型號和控制器)
  2. 嘗試使用--format documentation標誌運行您的規格以查看您的示例和上下文名稱是否有意義。在這種情況下,我相信你可以改進它。

而不是

describe "fails" do 
    it "to create without email" do 
    ... 
    end 
end 

它更有意義的寫

describe "when creating user without email" do 
    it "adds error" do 
    ... 
    end 
end 

等。您還可以添加例子像does not create new record

  • 而不是使用模型直接可以使用的工廠 - https://github.com/thoughtbot/factory_girl_rails

  • pass例子沒有測試任何東西。而不是寫這個

    user = User.create(...) user.save!

  • 當您使用的工廠我敢肯定,正確的規格應該是這樣的

    require 'rails_helper' 
    
    describe User do 
        let(:user) { FactoryGirl.build :user, first_name: 'First name', last_name: 'Last name', email: '[email protected]' } 
    
        context 'when user want to create account without email' do 
        before { user.email = nil } 
    
        it 'does not save user record' do 
         expect { user.save }.not_to change(User, :count) 
        end 
        end 
    end 
    
    +0

    謝謝@dziamber ...你的回答對我很有幫助....我有有些谷歌它跑這樣的規格。這是對的嗎? \t爲 「失敗沒有一個有效的電子郵件」 做 \t \t用戶= User.create(:如first_name => 「」:姓氏=> 「」:電子郵件=> 「測試@測試」:狀態=>「 「) \t \t預期(用戶)。爲了be_valid,拉姆達{‘有望有效的電子郵件’} \t年底 –

    +0

    請更新您的職位,而不是在評論粘貼代碼,因爲它現在是不是可讀 –

    0
    it "fails without email" do 
        user = User.create(:first_name => "test", :last_name => "test",:email => "") 
        user.errors.messages[:email][0].should eq "can't be blank" 
    end 
    
    it "fails without parameters" do 
        user = User.create(:first_name => "", :last_name => "", :email => "", :status => "") 
        user.errors.messages[:email][0].should eq "can't be blank" 
    end 
    
    it "fails without a valid email" do 
        user = User.create(:first_name => "", :last_name => "", :email => "[email protected]", :status => "") 
        expect(user).to be_valid, lambda { "expected valid email" } 
    end 
    
    it "passes without first name" do 
        pre_count = User.count 
        user = User.new 
        user.first_name = "" 
        user.last_name = "Test" 
        user.status = "active" 
        user.email = "[email protected]" 
        user.save! 
        count = User.count 
        tot_count = pre_count + 1 
        count.should be tot_count 
        user.email.should eq "[email protected]" 
        user.first_name.should eq nil 
        user.last_name.should eq "Test" 
        user.status.should eq :active 
    end 
    
    +0

    你檢查'FactoryGirl'寶石?另外一個好習慣是每個例子只有一個期望值 –

    +0

    sure @dziamber ...我會檢查它... –

    相關問題