I happened to mix ryan bates’ authentication scaffold with
rspec_scaffold on a demo project.
and ran into the problem of mixing mock frameworks…ryan uses mocha.
So, as a learning experience, I choose to redo ryan’s tests without
mocha but ran into a strange problem with tests of the User model.
With debugging you can see…
If you run just the user_spec.rb file, everything is fine…rspec
goes through the User model.
However, if you run all the specs, it is somehow still using mocha
instead of the User model.
How is this possible since I removed the configuration for mocha.?
In spec_helper.rb, I commented out the line: #
config.mock_with :mocha
I also saw a require ‘mocha’ statement in test_helper that I removed.
However, if you run all the specs, it is somehow still using mocha
instead of the User model.
How is this possible since I removed the configuration for mocha.?
In rails 2.3, activesupport/lib/active_support/test_case.rb requires
mocha. That’s the base class for all of the rails test cases, and
consequently, rspec example groups as well.
ok, I investigated further…and I’m still confused.
this is testing a typical authenticate class method…
login can be either username or email address
def self.authenticate(login, pass)
user = find_by_username(login) || find_by_email(login)
return user if user && user.matching_password?(pass)
end
the 2 tests that fail only in spec spec…
before :each do @user = User.new
end
it “should not authenticate bad username” do
#@user.attributes = valid_user_attributes @user = User.create!(valid_user_attributes)
debugger @find_user = User.authenticate(‘nonexisting’, ‘secret’) @find_user.should be_nil
end
it “should not authenticate bad password” do @user.attributes = valid_user_attributes @user.save!
User.authenticate(‘joseph’, ‘badpassword’).should be_nil
end
When you run the specific spec file, it works correctly, i.e. by
executing this class method.
When you run spec spec, it seems to mock the method with mocha (never
passes through this method!)
and returns the fixture object…which fails the test.
What is the correct way to do this?
… without using config.mock_with :mocha, of course
thanks
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.