簡體   English   中英

Rails-在涉及會話的情況下測試Cucumber + Capybara中資源的“編輯”操作

[英]Rails - Testing “edit” action from a resource in Cucumber+Capybara with session involved

我已經在路線中聲明了這一點:

resources :lessons

我正在嘗試使用以下功能來創建用於編輯課程的測試:

Feature: Edit Lesson
  As a logged in user of the website
  I want to edit a lesson
  so I can change the values on it

    Scenario: I am signed in and I edit an existing lesson
      Given I am logged in
      And I access the edit lesson page
      When I edit an existing lesson with correct values
      Then I should see a lesson edited message

然后,我執行以下步驟:

def valid_user
  @user ||= { :name => "Testy McUserton", :email => "testy@userton.com",
    :password => "please", :password_confirmation => "please", :description => "I love to play soccer"}
end

Given /^I am logged in$/ do
  sign_up valid_user
end

這是我有疑問的地方:

And /^I access the edit lesson page$/ do
  visit edit_lesson_path(???)
end

編輯課程路徑需要一個ID,因此我想應該“創建”一個課程,但是為了創建該課程,需要會話中當前用戶的user_id。 我應該如何在黃瓜+水豚上做到這一點? 我正在使用Devise管理身份驗證,用戶會話等。

這是我創建新課程的方式,以及為什么以某種方式需要user_id的原因:

def create
    @lesson = Lesson.new(params[:lesson])
    @lesson.user_id = current_user.id
    if @lesson.save
      redirect_to lesson_path(@lesson)
      flash[:notice] = "Congrats! Lesson has been created successfully."
    else
      render :action => "new"
    end
  end

在使用水豚黃瓜的測試案例中,您必須管理background:可以適用於整個功能的過程

之后,您必須獲取要對其進行編輯的數據

示例代碼可以像這樣

Backgound:
 Given the following users:
  | email          | company_name   | role  | confirmed | pending |
  | admin@test.com | xyz            | admin | true      | false   |
@javascript
Scenario: Specifying company
 Given "xyz" has the following lesson:
  | name    |
  | lesson1 |
 And I access the edit lesson page
 When I edit an "lesson1" with correct values
 Then I should see a "lesson1" edited message

這將幫助您在整個功能中都提到有關后台過程的課程,因為每次從同一功能調用場景時都會運行后台。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM