Reputation: 141
I have a nested resource specialty under user.
My routes.rb looks like
resources :users do
resources :specialties do
end
end
My factories.rb looks like
Factory.define :user do |f|
f.description { Populator.sentences(1..3) }
f.experience { Populator.sentences(1..5) }
f.tag_list { create_tags }
end
Factory.define :specialty do |f|
f.association :user
specialties = CategoryType.valuesForTest
f.sequence(:category) { |i| specialties[i%specialties.length] }
f.description { Populator.sentences(1..5) }
f.rate 150.0
f.position { Populator.sentences(1) }
f.company { Populator.sentences(1) }
f.tag_list { create_tags }
end
My Specialties_controller.rb looks like
class SpecialtiesController < ApplicationController
def index
@user = User.find(params[:user_id])
@specialties = @user.specialties
respond_to do |format|
format.html # index.html.erb
format.json { render json: @specialties }
end
end
My specialties_controller_spec.rb looks like
require 'spec_helper'
describe SpecialtiesController do
render_views
describe "GET 'index'" do
before do
@user = Factory.create(:user)
@specialty = Factory.create(:specialty, :user => @user)
@user.stub!(:specialty).and_return(@specialty)
User.stub!(:find).and_return(@user)
end
def do_get
get :index, :user_id => @user.id
end
it "should render index template" do
do_get
response.should render_template('index')
end
it "should find user with params[:user_id]" do
User.should_receive(:find).with(@user.id.to_s).and_return(@user)
do_get
end
it "should get user's specialties" do
@user.should_receive(:specialty).and_return(@specialty)
do_get
end
end
end
The first two tests pass, but the last test fails with the error message
Failure/Error: @user.should_receive(:specialty).and_return(@specialty)
(#<User:0x007fe4913296a0>).specialty(any args)
expected: 1 time
received: 0 times
Does anybody have an idea what this error means and how to fix it? I've looked at similar posts and can't find an error in my code. Thanks in advance.
Upvotes: 2
Views: 989
Reputation: 5110
@user.should_receive(:specialty).and_return(@specialty)
specialty
is a one-to-many relation and should be plural: specialties
. And indeed you have in your controller:
@specialties = @user.specialties
Upvotes: 1