I am trying to test my Rails 3.0.9 controller with Rspec 2.6.4 and Webrat 0.7.3. My controller looks like this:
#metrics_controller.rb
class MetricsController < ApplicationController
def show
@metric = Metric.all(:msrun_id => params[:id]).first
end
def index
@metrics = Metric.all
end
end
And my controller spec looks like this:
#metrics_controller_spec.rb
require 'spec_helper'
describe MetricsController do
describe "GET #index" do
it "should be successful" do
get :index
response.should be_success
end
end
describe "GET show" do
it 'contains an overview of a metric' do
get :show, :id => 1
response.should have_selector('title', :content => "Metric Overview")
end
end
end
This looks very similar to other examples I have seen in documentation, but when I run bundle exec rspec spec/controllers/metrics_controller_spec.rb I am getting some strange errors:
1) MetricsController GET #index should be successful
Failure/Error: response.should be_success
TypeError:
wrong argument type RSpec::Matchers::BePredicate (expected Proc)
# ./spec/controllers/metrics_controller_spec.rb:8
2) MetricsController GET show contains an overview of a metric
Failure/Error: response.should have_selector('title')
TypeError:
wrong argument type Webrat::Matchers::HaveSelector (expected Proc)
# ./spec/controllers/metrics_controller_spec.rb:16
It looks like something weird is going on with the response.should method. If I change the first example to something more verbose that doesn’t call should on response like this:
response.success?.should == true
then the example works fine, but why would should be expecting a Proc? Any ideas about how I can fix this?
This is not an especially helpful answer, but I will put it in here in case someone else gets stuck on the same thing. I inherited the project from someone else, and they set it up to use both railties and rails. Changing the Gemfile to look like this:
Instead of something like this:
along with changing the
config/application.rbto require the railties instead of rails seemed to fix it. The key seemed to be using railties instead of all of rails along with dm-rails.