2012-12-17 53 views
1

我知道這是一個很簡單的問題,但我不能似乎能夠解決這個問題,儘管許多變化的屬性:更新on Rails的

it "will send an email" do 
    invitation = Invitation.new 
    invitation.email = "[email protected]" 
    invitation.deliver 
    invitation.sent.should == true 
    end 

我的模型:

class Invitation 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    field :email, :type => String, :presence => true, :email => true 
    field :sent, :type => Boolean, :default => false 
    field :used, :type => Boolean, :default => false 
    validates :email, :uniqueness => true, :email => true 

    def is_registered? 
    User.where(:email => email).count > 0 
    end 

    def deliver 
    sent = true 
    save 
    end 

end 

這輸出:

1) Invitation will send an email 
    Failure/Error: invitation.sent.should == true 
     expected: true 
      got: false (using ==) 
    # ./spec/models/invitation_spec.rb:26:in `block (2 levels) in <top (required)>' 

如何設置值,然後將其保存在模型本身?

+0

似乎一切都很好,我會建議嘗試救人!在你的模型中。它似乎無法驗證。 –

回答

4

deliver方法不會做你認爲它的作用:

def deliver 
    sent = true 
    save 
end 

所有,它被設置局部變量senttrue然後調用save沒有任何東西被改變;沒有任何變化self.sent因此invitation.sent.should == true將失敗。

您想爲sent=方法提供一個明確的接收器,使紅寶石知道你不想分配到一個局部變量:

def deliver 
    self.sent = true 
    save 
end 
+0

謝謝!我總覺得「自我」是指班級,而不是實例,但顯然不是。 – Duopixel