[rspec-users] how to test a observer model?

Matt Wynne matt at mattwynne.net
Thu Nov 26 13:15:15 EST 2009


On 26 Nov 2009, at 02:31, Zhenning Guan wrote:

> I have a user_observer, code like this:
>
> def after_create(user)
> UserMailer.deliver_signup_notification(user)
> user.random_key = random_key_method
> user.save
> end
>
>
>  before do
>    @user = mock_model(User)
>    @user_observer = UserObserver.instance
>  end
>
> UserMailer.should_receive(:deliver_signup_notification).with(@user)
> @user_observer.after_create(@user)
> ---------------------------------------
>
> how can I test the random_key process?

You can do something like this to sense the value passed to the user  
object:

it "should generate a key for the user which is 30 characters long" do
    @user.should_receive(:random_key=) do |value|
       value.length.should == 30
    end
    @user_observer.after_create(@user)
end

Obviously the precise specifications of the key are up to you and your  
stakeholders, but that should show you how you can test it.

cheers,
Matt

http://mattwynne.net
+447974 430184



More information about the rspec-users mailing list