Expectations on Class Methods

Sorry about the non-specific subject. Here’s what I’m trying to do. I
have a method:

DataMapper::Database.setup

That I want to create an expectation on. I wrote:

DataMapper::Database.should_receive(:setup).once.and_return
(connection_hash)

The call to setup is invoked in the “Object” namespace. I.e., it is
setup code, and not in any method or class.

I can see that the class method is being called rather than the
expectation. Is there something obvious I’m missing?

Thanks,

–s

On Nov 27, 2007, at 3:13 PM, s.ross wrote:

The call to setup is invoked in the “Object” namespace. I.e., it is
setup code, and not in any method or class.

Try using “load” to load the file after the expectation (not with
require, before the expectation, as you would usually do). (see this
blog post by Jay Fields, which is about validations, but uses the
load trick: http://blog.jayfields.com/2006/12/rails-unit-testing-
activerecord.html)

Or, you could also do some sort of behaviour verification. What do
you expect this DatabaseMapper setup should do? Why is it in your
code? If you can answer these questions, then you can probably write
a test for it (but without stubbing)

Scott

On Nov 27, 2007, at 12:23 PM, Scott T. wrote:

Or, you could also do some sort of behaviour verification. What do
you expect this DatabaseMapper setup should do? Why is it in your
code? If you can answer these questions, then you can probably write
a test for it (but without stubbing)

Boy, don’t I wish. Not my code, but I’d like to have a spec before I
submit a patch. The DataMapper project is doing a great job of
keeping specs parallel to their development, but the one thing they
do is prepopulate a sqlite database prior to running all the tests.
The bug I’m submitting a patch for is mysql-specific, and I’m at a
loss how to un-sqliteize the environment without breaking all the
other tests.

I know tests should run in isolation, but again, not my specification
suite. They optimized for quick runs of the specs, I guess. So,
rather than spec the behavior, I’m opting to check whether setup is
called with the right parameters. What I’d prefer is to do something
more like:

DataMapper::Database[:default].socket.should eql(’/tmp/mysql.sock’)

which is the behavior I want. I’m not seeing how to get from here
to there.

The following is pretty much where I’m at, except that the
expectation for :setup has a with(). I put out a ping to the
DataMapper list, but so far no responses.

module DataMapper
class Database
end
end

describe “setting up datamapper from a database.yml” do
it “should look for database.yml” do
File.stub!(:exists?).and_return(true)
YAML.stub!(:load_file).and_return(
“development”=>{
“socket”=>"/tmp/mysql.sock",
“username”=>“root”,
“adapter”=>“mysql”,
“password”=>nil,
“database”=>“fakedb_development”}
)
DataMapper::Database.should_receive(:setup)
Kernel::load File.dirname(FILE) + ‘/…/lib/data_mapper.rb’
end
end