3

私はこの例を持っています:

# GET New
context "on get to new" do
  it "should assign cardset" do
    @profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset))
    get :new
    assigns[:cardset].should_not be_nil
  end
end

このメソッドをテストするには:

# GET /cardsets/new
def new
  @cardset = current_user.cardsets.build
end

私はcurrent_user、ユーザーが自分自身に属するものだけを作成していることを確認するために、関連付けが構築されていることを強制しようとしています。私は、オブジェクトfindから呼び出していることを確認するために非常によく似た期待値を使用しており、それが検索で機能しますが、上記の例を実行すると、次のようになります。current_user

6)
Mocha::ExpectationError in 'CardsetsController for a logged in user on get to new should assign cardset'
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: [#<Cardset:0x102eaa8c8>, #<Cardset:0x102e12438>].build(any_parameters)
satisfied expectations:
- allowed any number of times, not yet invoked: ApplicationController.require_user(any_parameters)
- allowed any number of times, already invoked twice: #<CardsetsController:0x1030849c8>.current_user(any_parameters)

/Applications/MAMP/htdocs/my_app/spec/controllers/cardsets_controller_spec.rb:32:
4

1 に答える 1

1

current_userから期待値を返す関数をスタブした後、@profileに期待値を追加します。おそらくあなたがする必要があるのはこれです:

# GET New
context "on get to new" do
  it "should assign cardset" do
    @profile.cardsets.expects(:build).once.returns(Factory.stub(:cardset))
    controller.stubs(:current_user).returns(@profile)
    get :new
    assigns[:cardset].should_not be_nil
  end
end
于 2011-11-03T18:19:30.583 に答える