2011-07-05 96 views
2

我被困在RSpec中的一個規範中。我如何更新模型的屬性使其成爲零?在更新控制器規範下更好地驗證?如何使用RSpec更新測試模型中的屬性

下面是一個示例代碼。

describe User do 
    describe ".validation" do 
    before(:each) do 
     @user = User.create!({ 
     :username => "dexter_morgan" 
     }) 
    end 

    ... 

    context "given invalid attributes" do 
     # how can I make the username nil? 
     it "rejects blank username" 
    end 
    end 
end 

回答

5

這應該夠了嗎?

describe User do 
    describe ".validation" do 
    before(:each) do 
     @user = User.create!({ 
     :username => "dexter_morgan" 
     }) 
    end 

    ... 

    context "given invalid attributes" do 
     it "rejects blank username" do 
     @user.username = nil 
     @user.should_not be_valid 
     end 
    end 
    end 
end 
+0

謝謝!有效! – Ben