You are currently browsing the tag archive for the ‘RSpec’ tag.

Just built this little snippet in a hacknight, it simulates a geolcation via capybara, so you can test if you geo-magic actually works :)

View

<p>Finding your location... <span id="status">checking...</span></p>
<script>
  jQuery(function () {
    var timeout = (document.location.href.indexOf('test_location') >= 0 ? 100 : 0);

    setTimeout(function(){
      navigator.geolocation.getCurrentPosition(function(){
        jQuery('#status').html("found you!");
      });
    }, timeout)
  });
</script>

Capybara test via selenium in rspec

  def simulate_location(lat, lng)
    page.driver.browser.execute_script <<-JS
      window.navigator.geolocation.getCurrentPosition = function(success){
        var position = {"coords" : { "latitude": "#{lat}", "longitude": "#{lng}" }};
        success(position);
      }
    JS
  end

  it "can use location", :js => true do
    visit '/?test_location=true'
    simulate_location 20, 20
    sleep 0.2
    page.should have_content "found you!"
  end

Make your rspec output look more interesting!
(yes we had too much spare time at our hands… :D )

# spec/spec_helper.rb
# encoding: UTF-8

if defined? RSpec::Core::Formatters::ProgressFormatter
  RSpec::Core::Formatters::ProgressFormatter.class_eval do
    DOTS = ['☘','⚘']
    def example_passed(example)
      output.print green(DOTS[rand(DOTS.size)])
    end
  end
end

So far our convention has been

  describe :method_name do 
    ...
  end

But this has some obvious drawback like separation of instance/class methods.
I hope we can now all agree on the unambiguous convention Mr Heinrich found in a great rspec presentation:

  • use “description” for non-methods
  • use pound “#method” for instance methods
  • use dot “.method” for class methods

This also makes prettier specdoc output :)

On my new project we had the problem that some specs failed when ran on their own, and some specs produces strange output (like “use object_id”), so I build a helper that runs each test on its own and could pinpoint the problem.

rake spec:one_by_one

it is now included in the single test rails plugin

What good is a dual-core when you do not use it !?

rake spec:parallel[1] –> 86 seconds
rake spec:parallel[2] –> 47 seconds
rake spec:parallel[4] –> 26 seconds

grab the parallel specs rails plugin here!

It requires a bit of setup, but hey: Can you spare 5 minutes now to save 1 minute 100 times a day ?

Follow

Get every new post delivered to your Inbox.

Join 76 other followers