Trying to write a failing test-- but am failing!

I am new to BDD, so am not quite sure how I am supposed to write a
test like this… I get:
“ActiveRecord::RecordInvalid in ‘User should fail when passwords do
not match’
Validation failed: Password doesn’t match confirmation”

If anyone can guide me in the right direction, I’d appreciate it…

-patrick

require ‘spec_helper’

describe User do

before(:each) do
@valid_attributes = {
:login => ‘test_name’,
:password => ‘password’,
:password_confirmation => ‘password’
}

  @invalid_attributes = @valid_attributes.merge(:password =>

‘not_the_same_password’)
end

it “should create a valid user” do
User.create!(@valid_attributes).should be_true
end

it “should fail when passwords do not match” do
User.create!(@invalid_attributes).should be_false
end

end

On Tue, Feb 16, 2010 at 5:42 PM, patrick99e99 [email protected]
wrote:

require ‘spec_helper’
@invalid_attributes = @valid_attributes.merge(:password =>
‘not_the_same_password’)
end

it “should create a valid user” do
User.create!(@valid_attributes).should be_true
end

it “should fail when passwords do not match” do
User.create!(@invalid_attributes).should be_false
end

The create! method is designed to raise an error if validation fails.
You can get this to work in one of two ways:

User.create(@invalid_attributes).should be_false # using create
instead of create!

or

expect do
User.create!(@invalid_attributes)
end.to raise_exception(ActiveRecord::RecordInvalid)

HTH,
David

Hi,

User.create(@invalid_attributes).should be_false # using create
instead of create!

Hmmm… when I tried this, I get:

‘User should fail when passwords do not match’ FAILED
expected #<User id: nil, login: “test_name”, crypted_password:
“e75e945946e0b31163c09da22ab0e8b725a89301d82275d0a90…”,
password_salt: “ivSY-ZjPwOiOoRfPfvAR”, persistence_token:
“9fa3b87bd2b8eb0d2ed600a0009f00ea4224de07954e96d7197…”,
single_access_token: “dGscUBSr_qJ3TTcHXpT3”, perishable_token: nil,
login_count: 0, failed_login_count: 0, last_request_at: nil,
current_login_at: nil, last_login_at: nil, current_login_ip: nil,
last_login_ip: nil, contact_id: nil, access_level_id: nil, active:
false, created_at: nil, updated_at: nil> to be false

expect do
User.create!(@invalid_attributes)
end.to raise_exception(ActiveRecord::RecordInvalid)

And when I tried it this way, I got the same error as before–

ActiveRecord::RecordInvalid in ‘User should create a valid user’
Validation failed: Password doesn’t match confirmation

-patrick

Hi,

The create! method is designed to raise an error if validation fails.
You can get this to work in one of two ways:

User.create(@invalid_attributes).should be_false # using create
instead of create!

Hmm, when I try that-- It still doesn’t work…

it “should fail when passwords do not match” do
User.create(@invalid_attributes).should be_false
end

‘User should fail when passwords do not match’ FAILED
expected #<User id: nil, login: “test_name”, crypted_password:
“7dc87663cf2600d1e14f8008b99beb458428aad4331765da0e9…”,
password_salt: “UITwJqOSRNzvajFmKpxL”, persistence_token:
“151252fc81a9990f8acbf689108b9a51239a9aa81f459ca8c75…”,
single_access_token: “ZISHX20gtD4gCowZ2NLo”, perishable_token: nil,
login_count: 0, failed_login_count: 0, last_request_at: nil,
current_login_at: nil, last_login_at: nil, current_login_ip: nil,
last_login_ip: nil, contact_id: nil, access_level_id: nil, active:
false, created_at: nil, updated_at: nil> to be false

expect do
User.create!(@invalid_attributes)
end.to raise_exception(ActiveRecord::RecordInvalid)

And when I tried that way, I got the same error I was getting
originally:

it “should fail when passwords do not match” do
expect
do
User.create!
(@invalid_attributes)
end.to raise_exception(ActiveRecord::RecordInvalid)
end

ActiveRecord::RecordInvalid in ‘User should create a valid user’
Validation failed: Password doesn’t match confirmation

-p

Hi again,

user = User.create(@invalid_attributes)
user.should_not be_valid

This gives me:

NoMethodError in ‘User should fail when passwords do not match’
undefined method `handling_predicate!’ for #<Spec::Matchers::Be:
0x104d8a518 @args=[:be_valid]>

Try raise_error instead of raise_exception. If that works, you’re
using an older version of rspec. If not, I’m not sure what’s going on.

Yeah, I wasn’t carefully reading that error-- it was:
ActiveRecord::RecordInvalid in ‘User should create a valid user’

I accidentally had @invalid = @valid.merge!(…new password…), and
that was breaking the should be valid test, the invalid one works with
the expect block. So that way works… Thank you.

-patrick

On Tue, Feb 16, 2010 at 8:27 PM, patrick99e99 [email protected]
wrote:

Hi again,

user = User.create(@invalid_attributes)
user.should_not be_valid

This gives me:

NoMethodError in ‘User should fail when passwords do not match’
undefined method `handling_predicate!’ for #<Spec::Matchers::Be:
0x104d8a518 @args=[:be_valid]>

This suggests that you’re mixing versions of rspec and rspec-rails
that are incompatible. What versions are you using, and do you have
any installed in vendor/plugins in addition to vendor/gems?

On Tue, Feb 16, 2010 at 8:02 PM, patrick99e99 [email protected]
wrote:

Hi,

User.create(@invalid_attributes).should be_false # using create
instead of create!

Hmmm… when I tried this, I get:

Oops - it’s this:

user = User.create(@invalid_attributes)
user.should_not be_valid

false, created_at: nil, updated_at: nil> to be false

expect do
User.create!(@invalid_attributes)
end.to raise_exception(ActiveRecord::RecordInvalid)

And when I tried it this way, I got the same error as before–

ActiveRecord::RecordInvalid in ‘User should create a valid user’
Validation failed: Password doesn’t match confirmation

Try raise_error instead of raise_exception. If that works, you’re
using an older version of rspec. If not, I’m not sure what’s going on.

This suggests that you’re mixing versions of rspec and rspec-rails
that are incompatible. What versions are you using, and do you have
any installed in vendor/plugins in addition to vendor/gems?

Well, I did run rake gems:unpack:dependencies, which I believe is what
the rspec book had said to do…

So, there is rspec and rspec-rails in vendor/gems

both are 1.2.9… but if I do spec --version, I get 1.3.0

-patrick

What command are you running?

I have just been doing script/autospec

… I actually posted on the rspecbook forum (haven’t heard anything
yet, so I’ll ask here) regarding autospec… When I ran it, it told me
that features were being skipped unless I supplied an environment
variable AUTOFEATURE=true… So, I exported that variable in my
bashrc file, but I find that my tests automatically rerun themselves
every second with autofeature turned on… So even if I am not
updating/saving files, the tests just keep getting executed over and
over… Is there a reason for this? When I watched BDDCasts, they
were using autospec with their features, and it didn’t appear to
behave that way for them, so I was wondering.

-patrick

On Tue, Feb 16, 2010 at 8:55 PM, patrick99e99 [email protected]
wrote:

This suggests that you’re mixing versions of rspec and rspec-rails
that are incompatible. What versions are you using, and do you have
any installed in vendor/plugins in addition to vendor/gems?

Well, I did run rake gems:unpack:dependencies, which I believe is what
the rspec book had said to do…

So, there is rspec and rspec-rails in vendor/gems

both are 1.2.9… but if I do spec --version, I get 1.3.0

What command are you running?

On Tue, Feb 16, 2010 at 10:24 PM, patrick99e99 [email protected]
wrote:

updating/saving files, the tests just keep getting executed over and
over… Is there a reason for this? When I watched BDDCasts, they
were using autospec with their features, and it didn’t appear to
behave that way for them, so I was wondering.

That’s likely because cucumber is generating a rerun.txt file and
autotest is picking that up for some reason.

That’s likely because cucumber is generating a rerun.txt file and
autotest is picking that up for some reason.

Ah… I do see that in my project directory there is a rerun.txt
file… How did that get created, and is the solution just to delete
it?

-patrick

I also get the infinite looping of the cucumber features being run.

The way I stopped it is by adding this:

Autotest.add_hook :initialize do |at|
%w{.svn .hg .git vendor rerun.txt}.each {|exception|
at.add_exception(exception)
}
end

to ~/.autotest

Is there a better to do this?

On Wed, Feb 17, 2010 at 12:24 PM, patrick99e99 [email protected]
wrote:

That’s likely because cucumber is generating a rerun.txt file and
autotest is picking that up for some reason.

Ah… I do see that in my project directory there is a rerun.txt
file… How did that get created, and is the solution just to delete
it?

Cucumber generates that for you. Take a look at your cucumber.yml file
and you should see some stuff about it. If you have any problems
please ask the cucumber mailing list for help:

http://groups.google.com/group/cukes

Cheers,
David