Forum: RSpec passing param values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mano a. (Guest)
on 2009-05-22 14:42
(Received via mailing list)
My controller spec is like below

describe UsersController do

 describe " create action" do
  before(:each)  do
    @user = mock_model(User,:user => "value")
    controller.stub!(:require_user).and_return(@user)
    controller.stub!(:uses_mailer).and_return(@user)
    User.stub!(:new).and_return(@user)
  end
  it "should create a new user object" do
      User.should_receive(:new).with(:user=>"value").and_return(@user)
      post :create,:user => "value"
  end
 end
end


Error:

<User(id: integer, created_on: datetime, updated_on: datetime,
destroyed_on: dat
etime, accessed_on: datetime, email_address: string, name_first: string,
name_la
st: string, mailing_address_id: integer, billing_address_id: integer,
name_displ
ay: string, picture_id: integer, url: string, country: string,
freelance: boolea
n, statement: string, hostname_active: boolean, hostname: string,
promotional_ma
ilings: boolean, sales_commission_rate: float, referral_commission_rate:
float,
employee_lock: boolean) (class)> expected :new with ({:user=>"value"})
once, but
 received it 0 times

How should I pass the params?


Diwa
David C. (Guest)
on 2009-05-22 15:06
(Received via mailing list)
On Fri, May 22, 2009 at 4:34 AM, Diwakar, ANGLER - EIT
<removed_email_address@domain.invalid> wrote:
> My controller spec is like below
>
> describe UsersController do
>
>  describe " create action" do
>   before(:each)  do
>     @user = mock_model(User,:user => "value")

#don't need to set the user in this
@user = mock_model(User)

>     controller.stub!(:require_user).and_return(@user)
>     controller.stub!(:uses_mailer).and_return(@user)
>     User.stub!(:new).and_return(@user)
>   end
>   it "should create a new user object" do
>       User.should_receive(:new).with(:user=>"value").and_return(@user)

#use 'user' instead of :user because Rails converts the keys
User.should_receive(:new).with('user' => 'value').and_return(@user)
Mano a. (Guest)
on 2009-05-22 16:08
(Received via mailing list)
Still I am getting the same error

describe UsersController do

 describe :create  do
  before(:each)  do
    controller.stub!(:require_user).and_return(@user)
    controller.stub!(:uses_mailer).and_return(@user)
    User.stub!(:new).and_return(@user)
     post :create,{:terms_and_conditions => "1",:promotional_mailings =>
"0",:email_address => "removed_email_address@domain.invalid"},:user => {}
  end
  it "should create a new user object" do
      User.should_receive(:new).with({"terms_and_conditions" =>
"1","promotional_mailings" => "0","email_address" =>
"removed_email_address@domain.invalid"}).and_return(@user)
      post :create,{:terms_and_conditions => "1",:promotional_mailings
=> "0",:email_address => "removed_email_address@domain.invalid"},:user => {}
  end
 end
end

Error

Spec::Mocks::MockExpectationError in 'UsersController create should
create a new
 user object'
<User(id: integer, created_on: datetime, updated_on: datetime,
destroyed_on: dat
etime, accessed_on: datetime, email_address: string, name_first: string,
name_la
st: string, mailing_address_id: integer, billing_address_id: integer,
name_displ
ay: string, picture_id: integer, url: string, country: string,
freelance: boolea
n, statement: string, hostname_active: boolean, hostname: string,
promotional_ma
ilings: boolean, sales_commission_rate: float, referral_commission_rate:
float,
employee_lock: boolean) (class)> expected :new with
({"terms_and_conditions"=>"1
", "promotional_mailings"=>"0", 
"email_address"=>"removed_email_address@domain.invalid"}) once,
but rec
eived it 0 times
David C. (Guest)
on 2009-05-27 07:45
(Received via mailing list)
On Fri, May 22, 2009 at 7:29 AM, Diwakar, ANGLER - EIT
<removed_email_address@domain.invalid> wrote:
>     post :create,{:terms_and_conditions => "1",:promotional_mailings => 
"0",:email_address => "removed_email_address@domain.invalid"},:user => {}
> Spec::Mocks::MockExpectationError in 'UsersController create should create a new
>  user object'
> <User(id: integer, created_on: datetime, updated_on: datetime, destroyed_on: dat
> etime, accessed_on: datetime, email_address: string, name_first: string, name_la
> st: string, mailing_address_id: integer, billing_address_id: integer, name_displ
> ay: string, picture_id: integer, url: string, country: string, freelance: boolea
> n, statement: string, hostname_active: boolean, hostname: string, promotional_ma
> ilings: boolean, sales_commission_rate: float, referral_commission_rate: float,
> employee_lock: boolean) (class)> expected :new with ({"terms_and_conditions"=>"1
> ", "promotional_mailings"=>"0", 
"email_address"=>"removed_email_address@domain.invalid"}) once, but rec
> eived it 0 times

Please show us the controller code.
This topic is locked and can not be replied to.