Forum: RSpec Troble running controller spec

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.
Amit K. (Guest)
on 2009-05-22 10:41
Hi,

    I am trying to execute controller spec by referring to RSpec book
but it is diving me error.

Following are the details:
Ruby version : 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
rspec - 1.2.4
rails - 2.1.2
OS - Vista Home

My channels_controller_spec.rb(which is under
rootproject/spec/controller directory) file contains:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ChannelsController do
  describe "Channel Creation" do
   before(:each) do
    @channel = mock_model(Channel, :save => nil)
    Channel.stub!(:new).and_return(@channel)
   end

   it "should create channel" do
    Channel.should_receive(:new).with(:brand_name => "test"
).and_return(@channel)
    post :create, :channel => { :brand_name => "test" }
  end

  it "should save channel" do
    @channel.should_receive(:save)
    post :create
  end
 end
end

Similarly my channels_controller.rb contains

def create
 @channel= Channel.new params[:channel]
 @channel.save
end

Now when i try to execute the spec it shows me error which i have
mentiond below along with its backtrace

FF.

1)
Spec::Mocks::MockExpecttionError in 'ChnnelsController Chnnel Cretion
should cre
te chnnel'
<Chnnel(id: integer, brnd_nme: string, logo_file_nme: string,
logo_file_size: st
ring, logo_content_type: string, points: integer, rnk: integer,
overruled_rnk: b
oolen, title: string, description: text, tgs: string, url: string,
kit_file_nme:
 string, kit_file_size: string, kit_content_type: string, ctive: boolen,
bg_colo
r: string, font_color: string, title_color: string, title_style: string,
emotion
l_file_nme: string, emotionl_file_size: string, emotionl_content_type:
string, n
cestor_id: integer, chnnel_ctegory_id: integer, creted_t: dtetime,
updted_t: dte
time) (clss)> expected :new with ({:brnd_nme=>"mnger"}) once, but
received it 0
times
./spec/controllers/chnnels_controller_spec.rb:11:
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/error_gen
ertor.rb:53:in `__rise'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/error_gen
ertor.rb:26:in `rise_expecttion_error'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/messge_ex
pecttion.rb:229:in `generte_error'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/messge_ex
pecttion.rb:192:in `verify_messges_received'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
190:in `verify_expecttions'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
189:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
189:in `verify_expecttions'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
66:in `verify'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/methods.r
b:34:in `rspec_verify'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/spce.rb:1
0:in `verify_ll'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/spce.rb:9
:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/spce.rb:9
:in `verify_ll'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/dpters/mock_frm
eworks/rspec.rb:13:in `verify_mocks_for_rspec'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_m
ethods.rb:129:in `fter_ech_exmple'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_m
ethods.rb:45:in `execute'
C:/Progrm Files/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_m
ethods.rb:37:in `execute'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:207:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:205:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:205:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:103:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/exmple_g
roup_runner.rb:23:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/exmple_g
roup_runner.rb:22:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/exmple_g
roup_runner.rb:22:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/options.
rb:119:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/commnd_l
ine.rb:9:in `run'
script/spec:4:

2)
Spec::Mocks::MockExpecttionError in 'ChnnelsController Chnnel Cretion
should sve
 chnnel'
Mock 'Chnnel_1002' expected :sve with (ny rgs) once, but received it 0
times
./spec/controllers/chnnels_controller_spec.rb:16:
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/error_gen
ertor.rb:53:in `__rise'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/error_gen
ertor.rb:26:in `rise_expecttion_error'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/messge_ex
pecttion.rb:229:in `generte_error'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/messge_ex
pecttion.rb:192:in `verify_messges_received'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
190:in `verify_expecttions'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
189:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
189:in `verify_expecttions'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/proxy.rb:
66:in `verify'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/methods.r
b:34:in `rspec_verify'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/spce.rb:1
0:in `verify_ll'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/spce.rb:9
:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/mocks/spce.rb:9
:in `verify_ll'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/dpters/mock_frm
eworks/rspec.rb:13:in `verify_mocks_for_rspec'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_m
ethods.rb:129:in `fter_ech_exmple'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_m
ethods.rb:45:in `execute'
C:/Progrm Files/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_m
ethods.rb:37:in `execute'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:207:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:205:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:205:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/exmple/exmple_g
roup_methods.rb:103:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/exmple_g
roup_runner.rb:23:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/exmple_g
roup_runner.rb:22:in `ech'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/exmple_g
roup_runner.rb:22:in `run'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/options.
rb:119:in `run_exmples'
C:/Progrm
Files/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.6/lib/spec/runner/commnd_l
ine.rb:9:in `run'

I had done same which is mentioned in the book still i dont know why i
am getting this error.
Please suggest :-)
David C. (Guest)
on 2009-05-22 15:27
(Received via mailing list)
On Fri, May 22, 2009 at 1:41 AM, Amit K. <removed_email_address@domain.invalid>
wrote:
>
>   end
>
>   it "should create channel" do
>    Channel.should_receive(:new).with(:brand_name => "test"
> ).and_return(@channel)

I *think* Rails converts the keys in this hash to strings. Try:

Channel.should_receive(:new).with('brand_name' => 'test').
  and_return(@channel)

HTH,
David
Amit K. (Guest)
on 2009-06-08 17:31
David C. wrote:
> On Fri, May 22, 2009 at 1:41 AM, Amit K. <removed_email_address@domain.invalid>
> wrote:
>>
>> � end
>>
>> � it "should create channel" do
>> � �Channel.should_receive(:new).with(:brand_name => "test"
>> ).and_return(@channel)
>
> I *think* Rails converts the keys in this hash to strings. Try:
>
> Channel.should_receive(:new).with('brand_name' => 'test').
>   and_return(@channel)
>

Hi,

I tried with the above command but it still gives the same error.Dont
know what is wrong.I am following the same which is given in the book
Details are as follows

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ChannelsController do
  describe "Channel Creation" do
    before(:each) do
      @channel = mock_model(Channel, :save => nil)
      Channel.stub!(:new).and_return(@channel)
    end

    it "should create channel" do
      Channel.should_receive(:new).with('brand_name' => 'manager'
).and_return(@channel)
      post :create, :channel => {'brand_name' => 'manager' }
    end

    it "should save channel" do
      @channel.should_receive(:save)
      post :create
    end
  end


Following are the errors
{Please ignore the missing a's :-)}

ChnnelsController
  Chnnel Cretion
    should crete chnnel (FILED - 1)
    should sve chnnel (FILED - 2)

1)
Spec::Mocks::MockExpecttionError in 'ChnnelsController Chnnel Cretion
should cre
te chnnel'
<Chnnel(id: integer, brnd_nme: string, logo_file_nme: string,
logo_file_size: st
ring, logo_content_type: string, points: integer, rnk: integer,
overruled_rnk: b
oolen, title: string, description: text, tgs: string, url: string,
kit_file_nme:
 string, kit_file_size: string, kit_content_type: string, ctive: boolen,
bg_colo
r: string, font_color: string, title_color: string, title_style: string,
emotion
l_file_nme: string, emotionl_file_size: string, emotionl_content_type:
string, n
cestor_id: integer, chnnel_ctegory_id: integer, creted_t: dtetime,
updted_t: dte
time) (clss)> expected :new with ({"brnd_nme"=>"mnger"}) once, but
received it 0
 times
./spec/controllers/chnnels_controller_spec.rb:183:
script/spec:4:

2)
Spec::Mocks::MockExpecttionError in 'ChnnelsController Chnnel Cretion
should sve
 chnnel'
Mock 'Chnnel_1002' expected :sve with (ny rgs) once, but received it 0
times
./spec/controllers/chnnels_controller_spec.rb:188:
script/spec:4:

Finished in 0.305 seconds

2 exmples, 2 filures
David C. (Guest)
on 2009-06-08 18:34
(Received via mailing list)
On Mon, Jun 8, 2009 at 8:31 AM, Amit K.<removed_email_address@domain.invalid>
wrote:
>> I *think* Rails converts the keys in this hash to strings. Try:
>
>      Channel.should_receive(:new).with('brand_name' => 'manager'
> ).and_return(@channel)
>      post :create, :channel => {'brand_name' => 'manager' }
>    end
>
>    it "should save channel" do
>      @channel.should_receive(:save)
>      post :create
>    end
>  end

I went as far as to create a rails 2.1.2 project with rspec and
rspec-rails 1.2.4, copy your code into it and ran the specs. They both
pass. I don't have a Vista environment in which to test this, so
that's all I can do to help at the moment.

Anybody else running into similar problems on Vista?

> 1)
> bg_colo
> script/spec:4:
> Finished in 0.305 seconds
>
> 2 exmples, 2 filures
Amit K. (Guest)
on 2009-06-08 18:48
David C. wrote:
> On Mon, Jun 8, 2009 at 8:31 AM, Amit K.<removed_email_address@domain.invalid>
> wrote:
>>> I *think* Rails converts the keys in this hash to strings. Try:
>>
>>      Channel.should_receive(:new).with('brand_name' => 'manager'
>> ).and_return(@channel)
>>      post :create, :channel => {'brand_name' => 'manager' }
>>    end
>>
>>    it "should save channel" do
>>      @channel.should_receive(:save)
>>      post :create
>>    end
>>  end
>
> I went as far as to create a rails 2.1.2 project with rspec and
> rspec-rails 1.2.4, copy your code into it and ran the specs. They both
> pass. I don't have a Vista environment in which to test this, so
> that's all I can do to help at the moment.
>
> Anybody else running into similar problems on Vista?
>

Thanks a lot for looking into the matter.I am not sure if i am getting
this problem due to vista environment.If it is then i dont know what to
do :-(

Also i didnt understand b the statement
"create a rails 2.1.2 project with rspec and rspec-rails 1.2.4, copy
your code into it and ran the specs."

Also i checked my spec version it is 1.2.6,so does this causing the
problem.
This version got updated from 1.2.4.
Please suggest.
David C. (Guest)
on 2009-06-08 19:03
(Received via mailing list)
On Mon, Jun 8, 2009 at 9:48 AM, Amit K.<removed_email_address@domain.invalid>
wrote:
>>>    it "should save channel" do
>> Anybody else running into similar problems on Vista?
>>
>
> Thanks a lot for looking into the matter.I am not sure if i am getting
> this problem due to vista environment.If it is then i dont know what to
> do :-(
>
> Also i didnt understand b the statement
> "create a rails 2.1.2 project with rspec and rspec-rails 1.2.4, copy
> your code into it and ran the specs."

The versions  you cited earlier were rails 2.1.2 and rspec 1.2.4, so I
did the following:

* created a rails 2.1.2 project
* configured it to use rspec 1.2.4
* created a ChannelsController and a spec for it
* created a Channel model with a migration and ran the migration
* copied the code you posted in this thread to
app/controllers/channels_controller.rb and
spec/controllers/channels_controller_spec.rb
* ran the specs
* watched them pass

>
> Also i checked my spec version it is 1.2.6,so does this causing the
> problem.
> This version got updated from 1.2.4.
> Please suggest.

You stated above that you were using 1.2.4, but now you are saying you
are using 1.2.6. Please be sure to provide accurate information.

Do you have both rspec and rspec-rails? And are they both 1.2.6 now?

FYI - I get the same result from 1.2.6. All specs pass.
Amit K. (Guest)
on 2009-06-08 19:10
> You stated above that you were using 1.2.4, but now you are saying you
> are using 1.2.6. Please be sure to provide accurate information.

I am sorry for the above information.Yes earlier the version was 1.2.4
but i installed rspec again and it installed the latest version i.e
1.2.6

> Do you have both rspec and rspec-rails? And are they both 1.2.6 now?
> Yes i have both rspec and rspec-rails as 1.2.6 and 1.2.4 installed.

> FYI - I get the same result from 1.2.6. All specs pass.
Amit K. (Guest)
on 2009-06-09 10:19
Amit K. wrote:
>> You stated above that you were using 1.2.4, but now you are saying you
>> are using 1.2.6. Please be sure to provide accurate information.
>
> I am sorry for the above information.Yes earlier the version was 1.2.4
> but i installed rspec again and it installed the latest version i.e
> 1.2.6
>
>> Do you have both rspec and rspec-rails? And are they both 1.2.6 now?
>> Yes i have both rspec and rspec-rails as 1.2.6 and 1.2.4 installed.
>
>> FYI - I get the same result from 1.2.6. All specs pass.

Any Updates....
:-)
David C. (Guest)
on 2009-06-09 16:00
(Received via mailing list)
On Tue, Jun 9, 2009 at 1:19 AM, Amit K.<removed_email_address@domain.invalid>
wrote:
>>
>>> FYI - I get the same result from 1.2.6. All specs pass.
>
> Any Updates....
> :-)

Any Vista users in earshot? I've done all I can do on this.
Amit K. (Guest)
on 2009-06-09 18:27
David C. wrote:
> On Tue, Jun 9, 2009 at 1:19 AM, Amit K.<removed_email_address@domain.invalid>
> wrote:
>>>
>>>> FYI - I get the same result from 1.2.6. All specs pass.
>>
>> Any Updates....
>> :-)
>
> Any Vista users in earshot? I've done all I can do on this.

Oh ok.
Mohit S. (Guest)
on 2009-06-09 21:29
(Received via mailing list)
David C. wrote:
>>>
> Any Vista users in earshot? I've done all I can do on this.
Hi David

I'm on Vista, but I just joined the mailing list - could I have a bit of
a background to what we're trying to do?  I'll try to see what I can do.

Cheers,
Mohit.
6/9/2009 | 11:30 PM.
David C. (Guest)
on 2009-06-09 21:33
(Received via mailing list)
On Tue, Jun 9, 2009 at 10:30 AM, Mohit S.<removed_email_address@domain.invalid> 
wrote:
>>>>>
>>>>>
> background to what we're trying to do?  I'll try to see what I can do.
Thanks for volunteering to help Amit. You can see the full thread at
http://groups.google.com/group/rspec/browse_thread....

Cheers,
David
Amit K. (Guest)
on 2009-06-10 10:48
Mohit S. wrote:
> David C. wrote:
>>>>
>> Any Vista users in earshot? I've done all I can do on this.
> Hi David
>
> I'm on Vista, but I just joined the mailing list - could I have a bit of
> a background to what we're trying to do?  I'll try to see what I can do.
>
> Cheers,
> Mohit.
> 6/9/2009 | 11:30 PM.

Thanks Mohit.
Amit K. (Guest)
on 2009-10-28 15:40
Hello,
Any updates on the above topic....
David C. (Guest)
on 2009-10-28 18:14
(Received via mailing list)
On Wed, Oct 28, 2009 at 8:40 AM, Amit K. <removed_email_address@domain.invalid>
wrote:

> Hello,
> Any updates on the above topic....


Please be sure to quote enough of the previous topic so those of us
reading
on phones can respond to you.

There is a thread about this on the Cucumber list - please see if this
information helps you:

https://rspec.lighthouseapp.com/projects/16211/tic...
http://wiki.github.com/aslakhellesoy/cucumber/trou... (Console
output isn’t right on Windows)
This topic is locked and can not be replied to.