[rspec-users] Loosing params and request info in 'if' statements

Kane Baccigalupi baccigalupi at gmail.com
Thu Feb 14 19:14:38 EST 2008


I have been a bad TDD developer and developed before testing. My code is
working fine, but I have been writing tests because it is the right thing to
do. I am running into trouble with some controller methods that are saved in
a separate lib module and included in the application.rb file. I started out
specing the methods from one of the controller specs and then when all went
well I moved them into a separate file and made the description shared. This
seemed to be working until I started testing a completely separate set of
method. Here is the code:

The specs:
# shared_member_restriction_spec.rb
describe "MemberRestriction", :shared => true do
 before(:each) do
    params[:username] = "some_user"
    params[:password] = "password"
    request.request_uri = "members/" + controller.controller_name

  describe "authenticate_if_in_members" do
    it "should call authenticate_if_in_members" do
      get :index

    it "should call authenticate_member" do
      get :index

# episode_controller_spec.rb
require File.dirname(__FILE__) + '/shared_member_restriction_spec'
describe EpisodesController do
  it_should_behave_like "MemberRestriction"
  # ...

And the lib module:
module MemberRestriction
  def authenticate_if_in_members
    if request.request_uri.include? "members/"
  # ...

Finally the episode controller code:
class EpisodesController < ApplicationController
  before_filter :authenticate_if_in_members
  # ...

The spec "should call authenticate_member" is failing like so:
Spec::Mocks::MockExpectationError in 'EpisodesController
authenticate_if_in_members should call authenticate_member'
Mock 'EpisodesController' expected :authenticate_member with (any args)
once, but received it 0 times

I thought maybe the problem was that really I hadn't gotten these specs
setup correctly for sharing, so I tried moving them back into the episode
controller spec. No luck. Then I thought that maybe this was a bug with the
version of rspec/autotest. I upgraded them both yesterday to the most recent

I ran into a similar problem testing a different module. I had some params
and a ruby statement that said something like
User.authenticate(params[:user], params[:password]) if params[:user] and
The error that I got said that params was nil and therefore nil.[] wasn't a
kosher ruby request. The if statement was redundant and so I took it out, at
which point the params were not nil and everything worked according to plan.
This was another situation where the code ran fine, but the tests were
coughing on an if statement.

I am honestly fuzzy about at what point rspec is setting the http head
variables, and how everything works, so please let me know if I am doing
something terribly or subtly wrong. I think it is my confusion about the
controller setup process that makes me avoid  BDD when it comes to my
controllers, and delight in it when it comes to my models.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080214/21f1b507/attachment.html 

More information about the rspec-users mailing list