stubs disapper

21 views
Skip to first unread message

sserdyuk

unread,
May 1, 2010, 10:04:42 AM5/1/10
to mocha-developer
Hi,

I am completely puzzled with the following situation. I stub an object
to avoid complex data setup, which normal implementation of
#last_statement requires. Stub works fine when I check it before
calling #charge!, but doesn't work inside #charge! call. Any ideas
anybody?
I am using bundler08 gem. My Gemfile and requires are pasted below.

class Account
def last_statement
nil # for simplicity
end
def charge!
if last_statement # stub doesn't work here
# should get here when running tests
CreditCardPayment.create!
end
end
end

class AccountTest < Test::Unit::TestCase
context "Account" do
setup do
@account = Factory(:good_trading_member).account
end
should "charge credit card when due" do
@account.stubs(:last_statement => true)
assert_not_nil @account.last_statement # stub works here
assert_difference "CreditCardPayment.count", 1 do
account.charge!
end
end
end
end

Gemfile:

clear_sources
source 'http://gemcutter.org'
source 'http://gems.github.com'

gem "rails", "2.0.2"
gem "mysql"
gem "image_science", "1.2.0"
gem "mime-types", "1.16"
gem "nokogiri", "1.3.3"
gem "prawn", "0.6.3"
gem "prawn-core", "0.6.3"
gem "prawn-format", "0.2.3"
gem "prawn-layout", "0.3.2"
gem "prawn-security", "0.1.1"
gem "RubyInline", "3.8.2"
gem "dictionary", "1.0.0"
gem "RedCloth", "3.0.4"

only :test do
gem "thoughtbot-shoulda", "2.10.2"
gem "factory_girl", "1.2.3"
gem "mocha", "0.9.8"
end

test_helper.rb begins with:

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/
environment")
require 'test_help'
require 'shoulda'
require 'mocha'
require 'factory_girl'; Factory.find_definitions

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

sserdyuk

unread,
May 2, 2010, 1:25:19 AM5/2/10
to mocha-developer
Apparently, it was some sort of incompatibility between FactoryGirl
and Mocha. Once I reloaded account instance with @account =
Account.find(Factory(:good_trading_member).account) all started
working. I wish I understood the cause better.

Vladimir Rybas

unread,
May 11, 2011, 3:34:51 AM5/11/11
to mocha-d...@googlegroups.com
Also I had a problem(just now) when tried to stub like

      Document.any_instance.stubs(:save).returns(:false)

to test construction like:

      if @document.save
         # scenario 1
      else
        # scenario 2
      end
 
to go right in scenario 2. However I stubbed as above, but scenario 1 is used, without any errors from Mocha.
I changed my stubbing to the syntax @sserdyuk using

      Document.any_instance.stubs(:save => false)

and scenario 2 is called now. So thanks dude )

James Mead

unread,
May 14, 2011, 5:47:36 AM5/14/11
to mocha-d...@googlegroups.com
I'm not sure it's the cause of your problem, but you should note that the require statements in your test helper are not controlling the load order of shoulda & mocha, since Bundler will automatically require them. It's usually a good idea to control the load order of shoulda & mocha because they both monkey-patch Test::Unit. The simplest way to do this is to add the ":require => false" option to the gem statements in the gem file. This way Bundler will not automatically load them and your requires in test helper will take effect.

    gem "thoughtbot-shoulda", "2.10.2", :require => false
    gem "mocha", "0.9.8", :require => false

Cheers, James.
----

James Mead

unread,
May 14, 2011, 5:49:48 AM5/14/11
to mocha-d...@googlegroups.com
On 2 May 2010 06:25, sserdyuk <sser...@gmail.com> wrote:
Apparently, it was some sort of incompatibility between FactoryGirl
and Mocha. Once I reloaded account instance with @account =
Account.find(Factory(:good_trading_member).account) all started
working. I wish I understood the cause better.

This seems very odd. Are you able to reproduce this behaviour in a simplified test e.g. create a blank Rails project with the minimum number of gems. If so, please submit it as an Github Issue and I'll look into it.

James Mead

unread,
May 14, 2011, 5:56:38 AM5/14/11
to mocha-d...@googlegroups.com
I think the source of your problem is that you are (probably unintentionally) using the symbol ":false" in your first example instead of the logical value "false". Then in your if statement, since the result of Document#save is now :false this will evaluate to true. So you can still use the first syntax style as follows :-

    Document.any_instance.stubs(:save).returns(false)

instead of :-

    Document.any_instance.stubs(:save).returns(:false)

Note the removal of the colon prefix from the return value.

I hope that helps.

Vladimir Rybas

unread,
May 14, 2011, 6:38:31 AM5/14/11
to mocha-d...@googlegroups.com
Yeah James, that was it. The ':false' instead of 'false'. Gosh ) Working now

Reply all
Reply to author
Forward
0 new messages