Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Archive for the ‘Ruby and Rails’ Category

Ruby on Rails Action Named “status” is Reserved

without comments

I just spent over an hour debugging a really frustrating problem. Apparently, defining a controller action as “status” is no good!

It will not break explicitly, but will create all kinds of weird chaos to occur. Please be advised!

Do NOT do this in a controller!

class MyController < ApplicationController
  ## DONT DO THIS!!!
  def status

Save yourself some headache :)

Written by Andrew Waage

October 3rd, 2012 at 12:01 am

Rails testing with Machinist 2, Rspec, Database Cleaner Gem

with one comment

QUICK vent and advice when using Machinst2 and Database Cleaner to test in Rails:


Add this to your environments/test.rb file:

Machinist.configure do |config|
  config.cache_objects = false

Machinist tries to do some weird caching to make your tests run faster. But, it doesn’t quite work the way you’d expect. If you are running into strange problems where your objects are persisting through many tests, even though you are using DatabaseCleaner after each test, you might try this. If you run into problems where running one test at a time works, but running “rake spec” results in errors, this is also worth a shot. Don’t let Machinist caching drive you nuts! :)

Sidenote: In my experience, the best way to debug these errors that appear when running the entire test suite, but do not appear when running individual tests is to use rspec to run all but one test. Remove one at a time, and see if removing that single test helps eliminate errors.

# If this gives errors:
$ bundle exec rspec ./spec/models/user_spec.rb ./spec/models/account_spec.rb ./spec/models/favorite_spec.rb
# Try removing the first
 $ bundle exec rspec ./spec/models/account_spec.rb ./spec/models/favorite_spec.rb
# Try removing the 2nd
 $ bundle exec rspec ./spec/models/user_spec.rb ./spec/models/favorite_spec.rb
# Repeat...

Written by Andrew Waage

April 11th, 2012 at 6:59 pm

Ruby floats, BigDecimals and money (currency)

with 3 comments

Fellow Ruby-ers, please be warned!!! DO NOT use Ruby floats when performing arithmetic calculations involving money!

My calculations work in IRB, so I was really confused when I ran into this weird situation where (what I thought was) a simple arithmetic calculation led to strange results in my unit tests (I cannot stress the importance of good unit testing!).

My backend calculation was basically this (simplified):

# arbitrary amounts for these two variables
percentage = 12
total_in_cents = 400

discount = percentage.to_f / 100.0
total_in_float = total_in_cents.to_f * 100.0
new_price = (total_in_float * discount ).round / 100

Now, it’s pretty obvious that 12% of (400 cents) $4.00 should just be $0.48 (48 cents)
However, my barrage of unit tests kept producing strange results where a simple calculation was returning incorrect results. Doing some research, I discovered a series of articles worth reading including:

Also, check out the Money gem – I’ve never used it personally, but people have said good things about it.

Heeding the advice I found online, I re-wrote all my money-related calculations using BigDecimals instead of Floats.

percentage = 12
total_in_cents = 400
discount = BigDecimal(percentage.to_s) / 100
total_in_float = BigDecimal(total_in_cents.to_s) * 100
new_price = (total_in_float * discount ).to_i / 100

After switching over from Floats to BigDecimals, my unit tests all passed!
Lesson learned and hope this heads-up helps you guys too.

Use BigDecimals for money calculations and remember to write good UNIT TESTS!!

Written by Andrew Waage

November 9th, 2011 at 5:29 pm

Rails 3 RSpec Request Spec – Testing Subdomains

without comments

How do you test sub-domains in RSpec Request specs (integration tests) ???

# Pass it into the GET request!
get '/programs/100', nil, {'HTTP_HOST' => ''}

The 3rd parameter to the get method is a hash of HTTP headers.
See the Rails API documentation for details.

Depending on the type of test you are working with (support / controller / request / integration etc.) you pass in the sub domain differently.

Here’s some good reference posts on Stack Overflow for setting subdomains in controller specs:
1. Rails RSpec Set Subdomain

# Set the in a before block
before(:each) do = "#{mock_subdomain}"

2. Subdomains in RSpec Controller Tests

 # I haven't tried this, and not sure you would need to mock out the current_subdomain method.
  @subdomain = ''
  controller.expects(:current_subdomain).returns(@subdomain) = "#{@subdomain}"

Written by Andrew Waage

July 11th, 2011 at 3:43 pm

RSpec – Running One Single Test at a Time

without comments

In the old days I would pass a regular expression to run a particular unit test or group of similarly named unit tests by name.

Here’s the easy way to run one test in RSpec… by line number!
Look at the line-number of any RSpec block (it, describe, etc), and simply run the rspec command, passing in the [filename]:[line number]:

$ rspec models/user_spec.rb:27

Happy testing!

Written by Andrew Waage

July 11th, 2011 at 3:03 pm