[rspec-users] stuck in testing hell...

Patrick J. Collins patrick at collinatorstudios.com
Tue May 1 03:24:31 UTC 2012


Hey everyone,

So here I am, trying to love my testing process-- but I really hit a
wall today and experienced a moment where testing totally causes a lack
of productivity...  To make a long story short, here's what happened:

I've been integrating a 3rd party payment service within my app-- The
way it works is, you get a token for the transaction, and then send a
request to their API to see if the credit card authorizes, and then if
it does, you can purchase it and make sure that's valid...

So I have a purchase model, and I am using state_machine to store the
transaction state...  For example, when an attempt to authorize a
credit card is done, if that fails, then the state machine transitions
within a failure block to set the purchase's state to
"authorization_failed".

...  Ok...  So, I made tests to cover all of this behavior, and
everything was perfect-- all tests were passing, and then I was told:
"Hey, we want to store the transaction data in the purchase records--
whether it failed or succeded"...  I said:  Sure!  No Problem!

The way I had this setup was, an attr_accessor :samurai_transaction

and then a before_create callback that calls a method
:build_samurai_transaction

... That method basically did:

Samurai::Processor.authorize( ...bunch o' auth data stuff... )

So to keep this data around, I made a migration to create the column
(same name as the attr_accessor property) and then I added serialize
:samurai_transaction and removed the attr_accessor macro.

Everything should be exactly the same behaviorally..  Ran my tests to
verify, and.. FAIL FAIL FAIL FAIL FAIL FAIL FAIL...

Inspecting the failures, I see that all of a sudden I am getting bogus
errors about "undefined method `matches_method?' for nil:NilClass"

I go through everything trying to figure out where this is coming from
and finally find that it's from my stubbing out the calls to Samurai..
I was doing:

auth = stub("auth", :success? => true, :capture => true)
Samurai::Processor.stubs(:authorize).returns auth

..  It worked fine before, but apparently something can't handle the
serialized database column holding a mocked object?  No idea...

So I changed this to:
auth = OpenStruct.new(:success? => true, :capture => true)

The error went away, which then made me convinced that it is something
to do with Mocha.....  However, then I saw other tests failing, and
guess why?

WRONG NUMBER OF ARGUMENTS FOR CAPTURE (0 for 1).

.... uhh?

Samurai has it's own ".capture" method to take funds..  This has some
conflicts with Rails' internal ".capture".........  For some reason,
o = OpenStruct.new(:capture => true)
o.capture
  => wrong number of arguments error...

So now I have to do:

o = OpenStruct.new(:kapture => true)
def o.capture
  kapture
end

and, suddenly I am finding myself in hacky hack land-- and hating every
minute of it.

Not to mention my tests still are breaking because of some other undetermined
problems having to do with OpenStruct not being as "simple" of a
solution as I was hoping.

... AAAAAAAAAAAAAAAAAAAAAAGH!


Patrick J. Collins
http://collinatorstudios.com



More information about the rspec-users mailing list