"user" received unexpected message :created_at with (no args)

My method looks like:

def self.can_do_this(user)
return false if user.nil?
( (Time.now >= user.created_at) ? true : false )
end

my spec:

it “should allow you to do this” do
user = stub(“user”)
user.stub(:nil?).and_return(false)
user.stub(:created_at).and_return(Time.now)

res = User.can_do_this(user)
res.should == true
end

Running the spec I get:

Failer/Error: res = User.can_do_this(user)
Stub “user” received unexpected message :created_at with (no args)

Any ideas?

Thanks allot for the help!

On Jun 29, 2011, at 12:21 PM, S Ahmed wrote:

user = stub(“user”)
Stub “user” received unexpected message :created_at with (no args)

Any ideas?

I copied what you have as/is and added the User class and example group
declarations:

class User
def self.can_do_this(user)
return false if user.nil?
( (Time.now >= user.created_at) ? true : false )
end
end

describe User do
it “should allow you to do this” do
user = stub(“user”)
user.stub(:nil?).and_return(false)
user.stub(:created_at).and_return(Time.now)

res = User.can_do_this(user)
res.should == true

end
end

This passes, so either something is different about our environments or
there is more going on than you are showing us. Please copy what I wrote
above into its own file and run it and let us know if it passes or
fails.

Off topic, some recommendations about the Ruby in your example:

  1. user.stub(:nil?).and_return(false) is unnecessary. It is an object
    and it will return false to nil?.
  2. you can declare a method stub in the declaration of the stub object:

user = stub(“user”, :created_at => Time.now)

  1. Asking an object if it is nil is unnecessary noise AND an unnecessary
    method call. This would be more terse and faster (as the evaluation
    happens in C in MRI, Java in JRuby, etc):

return false unless user

  1. A ternary that returns true or false is redundant. These two
    expressions are functionally equivalent:

(Time.now >= user.created_at) ? true : false
Time.now >= user.created_at

  1. Given #3 and #4, the implementation of can_do_this can be reduced to:

user && Time.now >= user.created_at

With those recommendations, the whole example can be reduced to:

class User
def self.can_do_this(user)
user && Time.now >= user.created_at
end
end

describe User do
it “should allow you to do this” do
user = stub(“user”, :created_at => Time.now)
User.can_do_this(user).should be_true
end
end

We could talk about can_do_this wanting a “?” at the end, but my
suspicion is that is not the real example you are working from.

HTH,
David

Your code worked, let me try and figure out why mine isn’t working…

Your right it isn’t exactly what I have, but pretty much everything is
identical except for the naming of the model.

Oh and I am grateful for your refactoring and explanations, simplies my
code
and tests (and understanding!)

Ok I got it working, and honestly I have no idea why it is working.

There was a test below it which I modified (I had updated_at instead of
created_at).

I ALMOST feel as if the file was cached and not looking at the right
file,
but not sure if that is even possible.