Trouble mocking with cucumber, rspec and rails

62 views
Skip to first unread message

Lucas Prim

unread,
Mar 19, 2010, 4:17:31 PM3/19/10
to Cukes
I'm having a lot of trouble trying to define a mock for a rails models
on cucumber. It seems like the method is creating a bunch of message
expectations and i keep getting errors like these:

----------------------------------------------------------------------------------------------------------------------------
Given I have only a product named "Sushi de Pato" # features/
step_definitions/product_
steps.rb:19
unexpected invocation:
#<Mock:ProductCategory_1001>.__mock_proxy()
unsatisfied expectations:
- expected exactly once, not yet invoked:
#<Mock:ProductCategory_1001>.errors(any_pa
rameters)
- expected exactly once, not yet invoked:
#<Mock:ProductCategory_1001>.id(any_parame
ters)
- expected exactly once, not yet invoked:
#<Mock:ProductCategory_1001>.to_param(any_
parameters)
- expected exactly once, not yet invoked:
#<Mock:ProductCategory_1001>.new_record?(a
ny_parameters)
- expected exactly once, not yet invoked:
#<Mock:ProductCategory_1001>.destroyed?(an
y_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked:
#<Mock:errors>.count(any_parameters)

(Mocha::ExpectationError)
----------------------------------------------------------------------------------------------------------------------------

I haven't yet implemented the ProductCategory class and I just want it
to return an ID and a 'name' attribute.

This is my step definition:

----------------------------------------------------------------------------------------------------------------------------
Given /^I have only a product named "([^\"]*)"$/ do |name|
@product = Product.create!(:name => name, :description =>
'Foo', :price => 100, :points => 100, :category =>
mock_model(ProductCategory))
end
----------------------------------------------------------------------------------------------------------------------------

And this is my env.rb file:

----------------------------------------------------------------------------------------------------------------------------
$: << File.join(File.dirname(__FILE__),"..")

require 'spec\spec_helper
----------------------------------------------------------------------------------------------------------------------------

I am using RSPec 1.3.0, cucumber 0.6.3 and webrat 0.7.0

I've tried to use stubs as well but got some other errors instead...

David Beckwith

unread,
Mar 19, 2010, 9:35:19 PM3/19/10
to cu...@googlegroups.com
Here is what I do in env.rb:

require "spec/mocks"

Before do
# To get RSpec stubs and mocks working.
$rspec_mocks ||= Spec::Mocks::Space.new
end

After do
begin
$rspec_mocks.verify_all
ensure
$rspec_mocks.reset_all
end
end

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
>
>

Lucas Prim

unread,
Mar 20, 2010, 3:58:10 PM3/20/10
to cu...@googlegroups.com
It works like a charm now! Thank you very much, David!

Lucas Prim
lucas...@gmail.com
+55 (48) 9921-9303
+55 (48) 3334-1979
Reply all
Reply to author
Forward
0 new messages