2
我在用戶下有一個嵌套的資源專用。在Rails 3.1中用Rspec和Factory girl測試嵌套的資源控制器
我的routes.rb看起來像
resources :users do
resources :specialties do
end
end
我factories.rb看起來像
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
我Specialties_controller.rb看起來像
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
我specialties_controller_spec.rb看起來像
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
前兩個測試通過,但最後的測試失敗,出現錯誤消息
Failure/Error: @user.should_receive(:specialty).and_return(@specialty)
(#<User:0x007fe4913296a0>).specialty(any args)
expected: 1 time
received: 0 times
沒有任何人有一個想法,這個錯誤是什麼手段,以及如何要解決這個問題?我看過類似的帖子,在代碼中找不到錯誤。提前致謝。