Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Testing within an ActiveRecord block

Robbie Clutton
Thursday, July 19, 2012

Following on from the article about ActiveRecord blocks and a similar topics at the office book club, I’ve been trying to put some of these concepts into my code. I did find it difficult to figure out how to test within the block. Below I’ll outline what we did to test the code within the block.

Say we’ve got a Foo class which inherits from ActiveRecord.

class Foo < ActiveRecord::Base end 

When create a new foo, we can use a block to mutate the object.

Foo.create(args) do |foo| foo.x = 1 end 

In the test, we want to return a stub, but we can’t assert that x was called on that stub as the block won’t be executed.

Foo.should_receive(:create) { foo_stub } foo_stub.should_receive(:x=) # assertion fails 

We can use a block after the ‘should_receive’ call to get the block as a Proc and call it with the stub we want to run assertions on.

Foo.should_receive(:create) do |args, &block| block.call(foo_stub) end 

Now we can successfully test inside the block.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. bryanl says:

    If you ever get down to this level, don’t you feel like your tests will exactly mimic your implementation?

    July 20, 2012 at 6:44 am

  2. Robbie Clutton says:

    Granted it’s a trivial example, you may be doing something more exciting inside a block that you feel requires testing.

    July 20, 2012 at 7:13 am

Add New Comment Cancel reply

Your email address will not be published.

Robbie Clutton

Robbie Clutton
New York

Recent Posts

  • Stop leaky APIs
  • ElementalJS and SimpleBDD open source updates
  • Stop leaking ActiveRecord throughout your application
Subscribe to Robbie's Feed

Author Topics

api (1)
architecture (1)
ironblogger (10)
object-design (2)
rails (4)
rest (2)
elementaljs (2)
opensource (1)
simplebdd (2)
activerecord (2)
routing (1)
refinements (1)
ruby (5)
scala (2)
autolibs (1)
brew (1)
documentation (1)
rvm (1)
bdd (3)
rspec (4)
testing (8)
lean (2)
startup architecture (2)
sustainable archtiecture (1)
yagni (1)
metrics (1)
validation (1)
nosql (1)
nulldb (1)
javascript (3)
ci (2)
jasmine (1)
build (1)
agile (1)
review (1)
web (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >