Rspec,`post`在示例组中不可用:如何重构

时间:2016-01-25 15:02:37

标签: ruby-on-rails rspec

我正在写第一次测试。

我对此结构进行了大量测试,因为我正在测试api ...

require 'rails_helper'

Rails.describe "Surveys", type: :request do

  let!(:survey) { create :survey, :id => 10 }
  let!(:user) { create :employee, :id => 51 }

  context "post wihout params" do
    params = 'Question_SKU=8&Language=Italian'
    post "/api/v1/surveys/complete_survey", params do
      header "Content-Type", "application/x-www-form-urlencoded"
    end

    it "should respond 412" do
      expect(response).to have_http_status(412)
    end

    it "should not create associated record" do
      expect(survey.survey_results.count).to be 0  
    end
  end
end

对于每次测试我都必须更改参数并运行帖子...... 使用这种语法我有一个错误:

`post` is not available on an example group

我不想把帖子放在每个区块内,我有大约100个区块......

有没有办法避免重复?

1 个答案:

答案 0 :(得分:0)

您可以将post块放在before :each块中,如下所示:

before :each do
  params = 'Question_SKU=8&Language=Italian'
  post "/api/v1/surveys/complete_survey", params do
    header "Content-Type", "application/x-www-form-urlencoded"
  end
end

该块将在每个示例之前运行。如果您只想运行一次,请使用before :all