1

わかりましたので、私の主な問題は、メッセージングを処理するために Mailboxer をプロジェクトに実装し、そのテストを作成しようとしていることです。しかし、何度もつまずき続けます。いくつかの異なるスタブ/モックを試みましたが、進歩はありませんでした。

各アクションを実行するために必要なすべてのインスタンス変数を設定するために before_filters に依存する conversations_controller.rb があります。次に、コントローラーのアクションで、インスタンス変数を直接参照して、何らかのアクションを実行したり、特定のデータを返したりします。

別の before_filter で指定されたメールボックスの、before_filter で指定された「ボックス」内のすべての会話を返す index アクションの例を次に示します。

class ConversationsController < ::ApplicationController
    before_filter :get_user_mailbox, only: [:index, :new_message, :show_message, :mark_as_read, :mark_as_unread, :create_message, :reply_message, :update, :destroy_message, :untrash]
    before_filter :get_box

    def index
      if @box.eql? "inbox"
        @conversations = @mailbox.inbox
      elsif @box.eql? "sentbox"
        @conversations = @mailbox.sentbox
      else
        @conversations = @mailbox.trash
      end
    end

そしてフィルターの前に:

private
def get_user_mailbox
    @user = User.where(:user_name => user.user_name.downcase).where(:email => user.email.downcase).first_or_create
    @mailbox = @user.mailbox if @user
end

def get_box
    if params[:box].blank? or !["inbox","sentbox","trash"].include?params[:box]
      params[:box] = 'inbox'
    end
    @box = params[:box]
end

だから私は1つに2つの質問があると思います。最初に、インデックス アクションに必要な正しいデータ @mailbox、@user、および @box をテストで生成する方法を説明します。次に、@box を別の "inbox/sentbox/trash" に設定するための偽のパラメータを渡すにはどうすればよいですか。controller.index({box: "inbox"}) を試しましたが、常に「間違った引数 1 対 0」というメッセージが表示されます。

さまざまな方法で次のことを試しましたが、常に nil:class エラーが発生します。これは、インスタンス変数が正しく設定されていないことを意味します。

describe "GET 'index' returns correct mailbox box" do
  before :each do
    @user = User.where(:user_name => 'test').where(:email => 'test@test.com').first_or_create
    @mailbox = @user.mailbox
  end

  it "#index returns inbox when box = 'inbox'" do
    mock_model User
    User.stub_chain(:where, :where).and_return(@user)
    controller.index.should == @mailbox.inbox
  end
end
4

2 に答える 2