实例变量在黄瓜步骤中不可用

时间:2018-09-04 17:54:18

标签: ruby-on-rails cucumber

嗨,我正在研究Rails项目,我必须使用Cucumber测试API。我有一些实例变量来存储URL,令牌等。我的第一种情况可以正常工作,但是第二种情况由于实例变量的值为nil而失败。

功能文件:

Feature: Bill Reminder

  Scenario: Validate POST bill_reminder API with valid parameters
    When I make POST bill_reminder API call
    Then It should return success for bill_reminder

  Scenario: Validate GET bill_reminder API with valid login token
    when I send a GET request to bill reminder details
    Then the json response should have title bill reminder

步骤文件:

When(/^I make POST bill_reminder API call$/) do
  @url = 'http://example.com/api/bill_reminders'
  @params = {
              data: {
                type: "bill-reminders",
                attributes: {
                  title: "bill reminder",
                  all_day: "0",
                  start_date: "1409175049",
                  end_date: "1409175049"
                }
              }
            }
  @login_token = 'nev4hPOqyVCXGnOXLYPUhQ'
  @user_id = 402
end

Then(/^It should return success for bill_reminder$/) do
  header 'login_token', @login_token
  header 'user-id', @user_id
  response = post @url, @params

  response_body = JSON response.body
  @id = response_body['data']['id']
  expect(response.status).to eq(200)
end

When(/^I send a GET request to bill reminder details$/) do
  @url = "http://example.com/api/bill_reminders/#{@id}"
  @user_id = 402
  @login_token = 'nev4hPOqyVCXGnOXLYPUhQ'
end

Then(/^the json response should have title bill reminder$/) do
  header 'login_token', @login_token
  header 'user-id', @user_id

  response = get @url
  expect(response.status).to eq(200)
end

在这里,由于实例变量的值为nil,第二种情况“ json响应应具有标题提示提醒”失败。我不知道为什么在第二种情况下实例变量的值为nil。请帮我修复它。

1 个答案:

答案 0 :(得分:1)

在第二个 步骤定义中,您错过了方法中的下划线_

When(/^I send a GET request to bill_reminder details$/) do
  @url = "http://example.com/api/bill_reminders/#{@id}"
  @user_id = 402
  @login_token = 'nev4hPOqyVCXGnOXLYPUhQ'
end