0

テストしている特定のインスタンスに対して、何かの存在を「true」に設定したいと思います。現在存在しないため、その存在 == false です。

ここに私がこれまで持っているコードがあります。誰かが助けてくれることを願っています。

Invitations_Controller:

def join_request
  invitation_options = {recipient_id: current_user.id, project_id: @project.id, recipient_email: current_user.email}

  if ProjectInvitation.where(invitation_options).present?
    flash[:notice] = "You already sent a request to join this project."
    redirect_to :back
    return
  end

Invitations_controller_spec:

describe "Send Join Request" do
  before do
    @invitation_options = {:recipient_id => @user.id, :project_id => @project.id, :recipient_email => 'address@gmail.com'}
    ProjectInvitation.where(@invitation_options).present? == true # This is what I'm stuck on. Pretty sure this doesn't work.
  end
  context "if you already sent a request" do
    it "should tell you that you already sent a request" do
      response.should have_text("You already sent a request to join this project.")
    end
    it "should redirect you to the previous page" do
      response.should redirect_to(:back)
    end
  end
end
4

3 に答える 3

0

「where」メソッドは簡単にスタブ化できますが、このモデルのすべてのクエリがスタブ化されます。例:

Profile.stub(:where) { "555" }
Profile.where(:id => 5) #will return 555
Profile.where(:name => 'Phil') #will return 555

しかし、最良の方法は、FactoryGirl を使用して ProjectInvitation を生成することです

于 2013-05-28T10:02:09.503 に答える
0

スタブ current_user メソッド。factory でプロジェクトを作成してから、仕様に {recipient_id: current_user.id, project_id: @project.id, recipient_email: current_user.email} という属性を持つ ProjectInvitation を作成するだけです。

于 2013-05-28T09:11:53.723 に答える
0

このクエリ ( ProjectInvitation.where(invitation_options).present?) をクラス メソッドに抽出ProjectInvitationし、テストでスタブすることを検討してください。

于 2013-05-28T09:00:44.450 に答える