[rspec-users] before_save model callback rspec testing
Pat Maddox
pergesu at gmail.com
Fri Jun 20 11:28:56 EDT 2008
Hey,
This isn't doing much to test the *behavior* of the object. Why do
you want to encrypt the password? Probably so you can authenticate,
right? I would probably start off with
describe "authenticate" do
it "finds the user with the given credentials" do
u = User.create!(:login => "pat", :password => "password")
User.authenticate("pat", "password").should == u
end
end
That might be a bit much to chew at first though. So you can write
some interim tests that you then throw away. For example, you might
do
describe User, "when saved" do
it "should create a salt" do
u = User.create(:login => "pat", :password => "password")
u.salt.should_not be_blank
end
it "should create a hashed pass" do
u = User.create(:login => "pat", :password => "password")
u.hashed_pass.should_not be_blank
end
end
Once you have those passing, you can move to the User.authenticate
spec. Once *that* passes, you can throw away the salt/hashed_pass
specs, because they're no longer useful. They're testing
implementation at this point, and were just a tool to get you where
you wanted to go in small steps.
Pat
On Fri, Jun 20, 2008 at 8:15 AM, Yi Wen <hayafirst at gmail.com> wrote:
> user.should_receive(:encrypt_password).with(your_password)
> user.save
>
> Is this what you want?
>
> On Fri, Jun 20, 2008 at 10:12 AM, Csongor Bartus <lists at ruby-forum.com> wrote:
>> hi all,
>>
>> i'm learning rspec and i can't figure out how to test if a callback is
>> executed in a model.
>>
>> my model code is:
>>
>> class User < ActiveRecord::Base
>> before_save :encrypt_password
>>
>> ...
>>
>> def encrypt(password)
>> self.class.encrypt(password, salt)
>> end
>>
>>
>> thanks a lot,
>> cs.
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
More information about the rspec-users
mailing list