0

現在選択されている会社をセッション変数に保存したいRails 3プロジェクトがあります。

私はスタッフ コントローラーの仕様に取り組んでおり、スタッフの新しいコントローラー アクションの仕様の例を分離しているため、現在の会社をスタブアウトしたいと考えています。

it "should call current_company" do 
  company = mock_model(Company, :id => "1")
  controller.should_receive(:current_company).and_return(company)
  get :new
end

スタッフコントローラーの新しいアクションは次のとおりです

  def new
    @staff = Staff.new
    @staff.company_id = current_company.id
  end

エラーが発生し続けます

Failure/Error: get :new
     NameError:
       undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>

また、 should_receive を使用する代わりにスタブアウトしようとしました

  controller.stub!(:current_company).and_return(company)

同じエラーが発生します。

4

2 に答える 2

0

あなたのコードは私には問題ないように見えますが、うまくいくはずです。私たちが見ていない他の問題があるに違いありません。コントローラー名が「StaffsController」になっていることに気付きましたが、正しいですか? コントローラーの名前と対応する仕様を再確認してください。それらは同じである必要があります。

于 2011-02-22T06:31:51.400 に答える
0

「成功するはず」の例/テストで爆撃していたと思うので、スタブを before ブロックに入れました。

require 'spec_helper'

describe StaffsController do

  describe "GET 'new'" do
    let(:staff) { mock_model(Staff, :company_id= => nil)}
    let(:company) { mock_model(Company, :id => 1)}

    before do
      Staff.stub!(:new).and_return(staff)
      controller.stub!(:current_company).and_return(company)
    end

    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should call current_company" do 
      controller.should_receive(:current_company).and_return(company)
      get :new
    end
  end
end

これは次の場合に機能します。

class StaffsController < ApplicationController
  def new
    @staff = Staff.new
    current_company.id
  end
end
于 2011-02-23T01:21:08.887 に答える