Stephan Hagemann's blog



TL;DR

We moved a Rails app into an unbuilt engine of a new blank slate container app to allow new parts of our app to live next to it as engines as well. It has been working great for us!

I have a sample app rails_container_and_engines of the result's structure on github.

Skip to the pitfalls and discoveries section to read about some of the speed bumps we during our transition. Interested in the why and how? Read on!

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

Stephan HagemannStephan Hagemann
IntelliJ Modules in Rubymine
edit Posted by Stephan Hagemann on Tuesday February 07, 2012 at 06:06PM

IntelliJ has a feature called modules: "a functional unit which you can compile, run, test and debug independently."

Modules in IntelliJ: Multiple top-level folders

A module in IntelliJ is a top-level view on a part of a codebase. IntelliJ is for Java, which is why I do not typically use it. I use Rubymine - no similar functionality exists here... but a way around that!

Stephan HagemannStephan Hagemann
Test your Rake tasks!
edit Posted by Stephan Hagemann on Sunday February 05, 2012 at 10:05AM

There are several reasons why you should test your Rake tasks:

  • Rake tasks are code and as such deserve testing.
  • When untested Rake tasks have a tendency to become overly long and convoluted. Tests will help keep them in bay.
  • As Rake tasks typically depend on your models, you (should) loose confidence in them if you don't have tests and are attempting refactorings.

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.

Stephan HagemannStephan Hagemann
Another first four weeks: concerned and delegated
edit Posted by Stephan Hagemann on Thursday November 10, 2011 at 12:58PM

James' post from a couple of weeks ago inspired me to write up my own experiences of my first couple of weeks at Pivotal. However, instead of telling you how it felt, I will tell you about stuff I learned.