Stephan HagemannStephan Hagemann
Never use shared examples groups! Usually.
edit Posted by Stephan Hagemann on Friday March 09, 2012 at 12:45PM

Shared example groups are a feature of rspec that allow specifying common behavior in a reusable group of specs.

I believe that there is a very specific way in which one can benefit from shared examples groups (and should keep them) and many more in which they come in handy at some point and should be refactored away from as development continues.

Stephan HagemannStephan Hagemann
Specific interfaces - in the small
edit Posted by Stephan Hagemann on Wednesday March 07, 2012 at 11:55AM

Everyone on the Web I found who states that quote I was looking for says "I don't know who said it, but be 'Generous on input, strict on output'" (or some variation on this). While I am unsure about the first proposition, I wholeheartedly agree with the second.

Stephan HagemannStephan Hagemann
Boulder Standup - Feb 16, 2012
edit Posted by Stephan Hagemann on Thursday February 16, 2012 at 08:09AM

rspec should render_template still behaving weirdly

Specing a partial being rendered with render_template blows up. What to do?

render_template has been brittle for a long time. It obviously still is:

response.should render_template("template") #works fine
response.should render_template(partial: "template") #works fine
response.should render_template(partial: "template", locals: {local_array: []}) #blows up within rspec

You know how to fix it? Solve it here

RubyMine 4 is out

RubyMine "compare two files" isn't broken

It is just really, really weird: if you have to scroll when comparing two files check the file that is above in the project drawer first. If you scroll up to do select the second file to compare, the compare window won't open.

Stubbing can? can be hard

In a system with a lot of cancan abilities - what is the best way to stub a particular ability for controller specs? In the hierarchy of controllers, a lot of abilities may be checked, all of which would need to stubbed in order to get to the code under test.

Instead of stubbing the abilities, create a new, anonymous, ability class specific for your spec that gets all the necessary abilities and then stub current_ability to return an instance of that class. Internally cancan calls current_ability when you call can?: cancan controller additions

Grant HutchinsGrant Hutchins
Standup 01/09/2012: Lightning and Rakes
edit Posted by Grant Hutchins on Tuesday January 10, 2012 at 06:52AM

Help

"RSpec 2.8 is out. The Rake runner in TeamCity isn't yet working."

The suggestion was to update to an EAP release of TeamCity.

Events

Stephan HagemannStephan Hagemann
Standup 11/11/2011: Some funkinesses
edit Posted by Stephan Hagemann on Friday November 11, 2011 at 09:06AM

Interesting

  • rspec stub != stub!. stub! is an alias method for stub. There is however also a method stub that is an alias for double. If you try to stub a method on the test class (to stub it on the context), you should probably use the magic subject/helper/controller methods. If you don't, using self.stub(:name => 'result') will create a double, while self.stub!(:name => 'result') will stub the method as you would expect.

  • Asynchronous file creation and downloading: if an asynchronous process writes a file using File.open and f.write, an other process checking the presence of the file to determine whether it is already available for download, will deliver the empty file, if the file has been opened, but not yet written.

    • Workarounds:
      • if you have one write to the file only: check filesize.
      • update an ActiveRecord attribute after the file writing is completed and check against that.
  • == on DelegateClass: newing up an instance delegate_x of DelegateClass from object x, x == delegate_x, while of course x.class != delegate_x.class.

Keystroke of the day

  • Rubymine KOTD: The search+replace mode you reach via Cmd+r allows you to see recent searches by hitting the down arrow. If that doesn't work for you in Lion, hit Ctrl+h.

Glenn JahnkeGlenn Jahnke
Standup 2011.04.12: Tweeting Standup and RSpec
edit Posted by Glenn Jahnke on Tuesday April 12, 2011 at 09:28AM

Interestings

It was suggested that we should Tweet our standups as well as post them to our blog.

Helps

What is the overhead of an RSpec "it" block?

People seem to think the overhead is really negligible compared to Rails load time or making any DB calls whatsoever. People have seen whole codebases moved from TestUnit to RSpec with no notable speed slow-down.

Michael SchubertMichael Schubert
RSpec 2 Gotcha with polymorphic_path
edit Posted by Michael Schubert on Wednesday December 08, 2010 at 11:05AM

A quick gotcha we ran into when using polymorphic path and rspec2 today.

In a controller test we had an assertion:

response.should redirect_to(polymorphic_path(@some_object))

Which resulted in the following error

Failure/Error: Unable to find matching line from backtrace stack level too deep
# ~/.rvm/gems/ruby-1.9.2-p0@gemset/gems/rspec-expectations-2.1.0/lib/rspec/matchers/method_missing.rb:4

It turns out polymorphic path is not available in the controller test (but the usual object_path method is). As to why this throws a stack level too deep and not a method undefined looks like potentially another bug in rspec but the solution seemed to be to do the following in our spec_helper.b for Rails 3:

include Rails.application.routes.url_helpers

For Rails 2.x you'll want to use:

include ActionController::UrlWriter

I've opened an issue for this on github as well https://github.com/rspec/rspec-expectations/issues/issue/46

Interesting:

  • Tests won't run if rspec is included in the test group. rspec must be included in the development group, or both. When a test run is started the RAILS_ENV is development. After some initialization the RAILS_ENV is changed to test - a point at which the Gemfile has already been evaluated.

Alex ChaffeeAlex Chaffee
Upgrading your Rakefile from RSpec 1.3 to RSpec 2
edit Posted by Alex Chaffee on Tuesday October 26, 2010 at 11:12AM

I'm updating Erector to RSpec 2 and came across two problems for which solutions were surprisingly difficult to Google. Here are my (finally successful) results.


Problem:

no such file to load -- spec/rake/spectask

Before:

require "spec/rake/spectask"  # RSpec 1.3

After:

require "rspec/core/rake_task" # RSpec 2.0

Problem:

undefined method `spec_files=' for #<RSpec::Core::RakeTask:0x00000101550aa8>

Before:

# RSpec 1.3
Spec::Rake::SpecTask.new(:core) do |spec|
  spec.spec_files = FileList['spec/erector/*_spec.rb']
  spec.spec_opts = ['--backtrace']
end

After:

# RSpec 2.0
RSpec::Core::RakeTask.new(:core) do |spec|
  spec.pattern = 'spec/erector/*_spec.rb'
  spec.rspec_opts = ['--backtrace']
end

See also http://github.com/rspec/rspec-core/blob/master/Upgrade.markdown (curiously cloaked from Google searches for the above problem strings).

Kelly FelkinsKelly Felkins
Standup 7/20/2010: The Daily Rubymine
edit Posted by Kelly Felkins on Tuesday July 20, 2010 at 10:35AM

Interesting Things

  • RubyMine 2.0.2, refactor => extract partial == FAIL. If you select multiple divs and perform extract partial, the selected region is removed, but only the first complete div is included in the new partial.

  • Rspec/Rubymine focused tests. Rubymine attempts to run focused tests using the --example 'text' option. Rspec apparently finds the example group, and runs the examples that are directly a part of that example group, but does not include descendent example groups -- *which can lead you to think examples are passing that were not actually run*. Apparently this is fixed in rspec 2.

Ask for Help

"Display of time is off by an hour, presumably due to Daylight Saving Time"

The team is displaying time stored as utc in the database, using strftime, and the time is off by an hour.

"Binding Click to Checkbox with Jquery"

A team was trying to check the value of a checkbox during the click event, but getting the opposite value. They worked through it but was hoping to find a better solution.

"Why is there a new default for include_root_in_json for rails 3?"

Just curious.

"Fakeweb, Capybara w/Selenium Webdriver == end of file?"

Getting "end of file" failure on CI. There were a few suggestions:

  • There is a fork of fakeweb that allows it to ignore localhost.
  • Consider using Webmock instead of fakeweb.

Other articles: