How to mock parameters with Mocha

Hi folks,

I want to test the following:

In sites controller:

def create
@site = Site.new(params[:site])

In the functional test:

class SitesControllerTest < Test::Unit::TestCase
def setup
@controller = SitesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_create

How can I mock the parameters passed to the new method of the Site model
to test the create method of the sites controller?

Thank you!

On 22 Feb 2008, at 11:34, Alvaro P. wrote:

to test the create method of the sites controller?

If i’ve understood what you want, something like
Site.expects(:new).with( :name => ‘foo’, :location => ‘bar’)
will do the job.

Fred

Frederick C. wrote:

On 22 Feb 2008, at 11:34, Alvaro P. wrote:

to test the create method of the sites controller?

If i’ve understood what you want, something like
Site.expects(:new).with( :name => ‘foo’, :location => ‘bar’)
will do the job.

Fred

That I’ve already tried, but does not work:

NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Frederick C. wrote:

On 22 Feb 2008, at 11:49, Alvaro P. wrote:

Fred

That I’ve already tried, but does not work:

NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Where does that error occur ? You are aware that setting an
expectation completely replaces the original method ? (ie Site.new
will now return nil, unless you do Site.expects(:new).returns(x) in
which case it returns x)

Fred

That’s what I though, but the behaviour is different. The error happens
in the line when the method new is called:

def create
@site = Site.new(params[:site]) <— here

On 22 Feb 2008, at 11:49, Alvaro P. wrote:

Fred

That I’ve already tried, but does not work:

NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Where does that error occur ? You are aware that setting an
expectation completely replaces the original method ? (ie Site.new
will now return nil, unless you do Site.expects(:new).returns(x) in
which case it returns x)

Fred

On 2/22/08, Alvaro P. [email protected] wrote:

def test_create
post :create, :site => …

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/