[rspec-devel] [ rspec-Feature Requests-6791 ] stub return values for all instances of a class

noreply at rubyforge.org noreply at rubyforge.org
Sun Nov 18 21:01:15 EST 2007


Feature Requests item #6791, was opened at 2006-11-20 16:50
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6791&group_id=797

Category: mock module
Group: None
>Status: Closed
Priority: 3
Submitted By: David Chelimsky (dchelimsky)
Assigned to: Nobody (None)
Summary: stub return values for all instances of a class

Initial Comment:
I'd like to be able to do this:

SomeClass.stub_instance_method!(:method_name).and_return(specified_value)

(syntax just a suggestion)

This should set up a world in which all instances of SomeClass return specified_value for any calls to :method_name.

This differs from SomeClass.stub! in that it applies to instance methods rather than class methods.

----------------------------------------------------------------------

Comment By: Chad Humphries (spicycode)
Date: 2007-11-19 01:58

Message:
Moved to http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/28-6791-stub-return-values-for-all-instances-of-a-class#ticket-28-2

----------------------------------------------------------------------

Comment By: Carl Porth (badcarl)
Date: 2007-07-18 22:49

Message:
I'd also like to be able to do this:

SomeClass.new_instances do |instance|
  instance.stub!(:method_name_1).and_return(:value_1)
  instance.stub!(:method_name_2).and_return(:value_2)
end

----------------------------------------------------------------------

Comment By: Chris Anderson (jchris)
Date: 2007-01-18 01:29

Message:
Found a solution to my specific problem:


def login_stub(user)

  controller.stub!(:current_user).and_return(user)

end


Just in case someone happens by from a google search etc.

----------------------------------------------------------------------

Comment By: Chris Anderson (jchris)
Date: 2007-01-17 23:24

Message:
I agree. I'm trying to stub a login_as method for controller specs that won't rely on the database. I'd like to have any instance of ApplicationController stub :current_user and return my mock user. But this doesn't seem possible with the current library. Mocha has the feature, so I may lean on it for the time being.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6791&group_id=797


More information about the rspec-devel mailing list