Rails - 使用Cucumber + Capybara中的资源测试“编辑”操作

时间:2012-03-17 03:31:35

标签: ruby-on-rails cucumber capybara

我在路线中声明了这一点:

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。我怎么能在Cucumber + Capybara做这个?我使用设计来管理身份验证,用户会话等。

以下是我创建新课程的方法,以及为什么我需要以某种方式使用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

1 个答案:

答案 0 :(得分:1)

在使用水豚黄瓜的测试案例中,您必须管理可用于整个功能的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

这将帮助您通过每次从同一功能调用场景时背景运行的整个功能提及后台流程来吸取您的课程。