0

对于我正在测试的特定实例,我想将某些东西的存在设置为“真”。它当前不存在,因此它的存在 == false。

这是我到目前为止的代码。希望有人可以提供帮助。

在邀请控制器中:

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

在邀请控制器规范中:

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

存根 current_user 方法。使用工厂创建一个项目,然后在您的规范中使用属性 {recipient_id: current_user.id, project_id: @project.id, recipient_email: current_user.email} 创建 ProjectInvitation。

于 2013-05-28T09:11:53.723 回答
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

考虑将此查询 () 提取ProjectInvitation.where(invitation_options).present?到一个类方法中ProjectInvitation,然后在您的测试中存根它

于 2013-05-28T09:00:44.450 回答