Тест на огурец действует странным образом

Сначала он основан на Rails 3 на Action, поэтому я буду печатать все, что я пробовал, и ошибки, которые у меня есть, чтобы кто-нибудь мог мне помочь, и если есть какие-то важные файлы, которые я должен добавить к своему вопросу, скажите... также возможно название вопроса не достаточно хорошо, но я не знаю, какое название я должен дать огуречным вопросам.

В книге автор пытался скрыть редактирование и удаление ссылок, чтобы не подписанный пользователь и зарегистрированный пользователь не могли видеть его, кроме как администраторы.

для аутентификации он использует devise 1.4.3 и версию огурца 1.0.6, я нахожусь в главе 7 специально на 7.3.3.

Теперь шаги в книге говорят, что мы запишем тест на огурец для сокрытия ссылок редактирования и удаления, и это был его код: hidden_links.feature

Scenario: Edit project link is hidden for non-signed-in users
  Given I am on the homepage
  When I follow "TextMate 2"
  Then I should not see the "Edit Project" link

Scenario: Edit project link is hidden for signed-in user
  Given I am signed in as "user@ticketee.com"
  When I follow "TextMate 2"
  Then I should not see the "Edit Project" link

Scenario: Edit project link is shown to admin
  Given I am signed in as "admin@ticketee.com"
  When I follow "TextMate 2"
  Then I should see the "Edit Proeject" link

Scenario: Delete project link is hidden for non-signed-in users
  Given I am on the homepage
  When I follow "TextMate 2"
  Then I should not see the "Delete Project" link

Scenario: Delete project link is hidden for signed-in users
  Given I am signed in as "user@ticketee.com"
  When I follow "TextMate 2"
  Then I should not see the "Delete Project" link

Scenario: Delete project link is shown to admins
  Given I am signed in as "admin@ticketee.com"
  When I follow "TextMate 2"
  Then I should see the "Delete Project" link

затем, когда я попытался запустить тест, у меня возникла проблема с отсутствующей функцией (хотя в книге тест проходит, а затем должен быть перенесен в github, но для меня он не проходит), поэтому я добавил это в верхней части моего кода:

Feature: Hidden Links
  In order to clean up the user experience
  As the system
  I want to hide links from users who can't act on them

Теперь, когда я пытаюсь запустить тест, я получаю эту ошибку:

➜  ticketee git:(master) ✗ bin/cucumber features/hidden_links.feature
Rack::File headers parameter replaces cache_control after Rack 1.5.
Using the default profile...
Feature: Hidden Links
  In order to clean up the user experience
  As the system
  I want to hide links from users who can't act on them

  Scenario: Edit project link is hidden for non-signed-in users # features/hidden_links.feature:6
    Given I am on the homepage                                  # features/step_definitions/web_steps.rb:44
    When I follow "TextMate 2"                                  # features/step_definitions/web_steps.rb:56
      Unable to find link "TextMate 2" (Capybara::ElementNotFound)
      ./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
      features/hidden_links.feature:8:in `When I follow "TextMate 2"'
    Then I should not see the "Edit Project" link               # features/hidden_links.feature:9
      Undefined step: "I should not see the "Edit Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:9:in `Then I should not see the "Edit Project" link'

  Scenario: Edit project link is hidden for signed-in user # features/hidden_links.feature:11
    Given I am signed in as "user@ticketee.com"            # features/hidden_links.feature:12
      Undefined step: "I am signed in as "user@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:12:in `Given I am signed in as "user@ticketee.com"'
    When I follow "TextMate 2"                             # features/step_definitions/web_steps.rb:56
    Then I should not see the "Edit Project" link          # features/hidden_links.feature:14
      Undefined step: "I should not see the "Edit Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:14:in `Then I should not see the "Edit Project" link'

  Scenario: Edit project link is shown to admin  # features/hidden_links.feature:16
    Given I am signed in as "admin@ticketee.com" # features/hidden_links.feature:17
      Undefined step: "I am signed in as "admin@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:17:in `Given I am signed in as "admin@ticketee.com"'
    When I follow "TextMate 2"                   # features/step_definitions/web_steps.rb:56
    Then I should see the "Edit Proeject" link   # features/hidden_links.feature:19
      Undefined step: "I should see the "Edit Proeject" link" (Cucumber::Undefined)
      features/hidden_links.feature:19:in `Then I should see the "Edit Proeject" link'

  Scenario: Delete project link is hidden for non-signed-in users # features/hidden_links.feature:21
    Given I am on the homepage                                    # features/step_definitions/web_steps.rb:44
    When I follow "TextMate 2"                                    # features/step_definitions/web_steps.rb:56
      Unable to find link "TextMate 2" (Capybara::ElementNotFound)
      ./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
      features/hidden_links.feature:23:in `When I follow "TextMate 2"'
    Then I should not see the "Delete Project" link               # features/hidden_links.feature:24
      Undefined step: "I should not see the "Delete Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:24:in `Then I should not see the "Delete Project" link'

  Scenario: Delete project link is hidden for signed-in users # features/hidden_links.feature:26
    Given I am signed in as "user@ticketee.com"               # features/hidden_links.feature:27
      Undefined step: "I am signed in as "user@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:27:in `Given I am signed in as "user@ticketee.com"'
    When I follow "TextMate 2"                                # features/step_definitions/web_steps.rb:56
    Then I should not see the "Delete Project" link           # features/hidden_links.feature:29
      Undefined step: "I should not see the "Delete Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:29:in `Then I should not see the "Delete Project" link'

  Scenario: Delete project link is shown to admins # features/hidden_links.feature:31
    Given I am signed in as "admin@ticketee.com"   # features/hidden_links.feature:32
      Undefined step: "I am signed in as "admin@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:32:in `Given I am signed in as "admin@ticketee.com"'
    When I follow "TextMate 2"                     # features/step_definitions/web_steps.rb:56
    Then I should see the "Delete Project" link    # features/hidden_links.feature:34
      Undefined step: "I should see the "Delete Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:34:in `Then I should see the "Delete Project" link'

Failing Scenarios:
cucumber features/hidden_links.feature:6 # Scenario: Edit project link is hidden for non-signed-in users
cucumber features/hidden_links.feature:21 # Scenario: Delete project link is hidden for non-signed-in users

6 scenarios (2 failed, 4 undefined)
18 steps (2 failed, 4 skipped, 10 undefined, 2 passed)
0m0.187s

You can implement step definitions for undefined steps with these snippets:

Then(/^I should not see the "(.*?)" link$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Given(/^I am signed in as "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Then(/^I should see the "(.*?)" link$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Затем я добавил это в свой код, чтобы попытаться выполнить мой тест:

Background:
  Given there are the following users:
    | email              | password | admin |
    | user@ticketee.com  | password | false |
    | admin@ticketee.com | password | true  |
  And there is a project called "TextMate 2"

Затем я запустил тест и получил эту ошибку:

➜  ticketee git:(master) ✗ bin/cucumber features/hidden_links.feature
Rack::File headers parameter replaces cache_control after Rack 1.5.
Using the default profile...
Feature: Hidden Links
  In order to clean up the user experience
  As the system
  I want to hide links from users who can't act on them

  Background:                                  # features/hidden_links.feature:6
    Given there are the following users:       # features/step_definitions/user_steps.rb:1
      | email              | password | admin |
      | user@ticketee.com  | password | false |
      | admin@ticketee.com | password | true  |
    And there is a project called "TextMate 2" # features/step_definitions/project_steps.rb:1

  Scenario: Edit project link is hidden for non-signed-in users # features/hidden_links.feature:13
    Given I am on the homepage                                  # features/step_definitions/web_steps.rb:44
    When I follow "TextMate 2"                                  # features/step_definitions/web_steps.rb:56
      undefined method `admins_only' for #<#<Class:0x9c16994>:0x9d134b4> (ActionView::Template::Error)
      ./app/views/projects/show.html.erb:3:in `_app_views_projects_show_html_erb___713092924_82350660'

      ./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
      features/hidden_links.feature:15:in `When I follow "TextMate 2"'
    Then I should not see the "Edit Project" link               # features/hidden_links.feature:16
      Undefined step: "I should not see the "Edit Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:16:in `Then I should not see the "Edit Project" link'

  Scenario: Edit project link is hidden for signed-in user # features/hidden_links.feature:18
    Given I am signed in as "user@ticketee.com"            # features/hidden_links.feature:19
      Undefined step: "I am signed in as "user@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:19:in `Given I am signed in as "user@ticketee.com"'
    When I follow "TextMate 2"                             # features/step_definitions/web_steps.rb:56
    Then I should not see the "Edit Project" link          # features/hidden_links.feature:21
      Undefined step: "I should not see the "Edit Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:21:in `Then I should not see the "Edit Project" link'

  Scenario: Edit project link is shown to admin  # features/hidden_links.feature:23
    Given I am signed in as "admin@ticketee.com" # features/hidden_links.feature:24
      Undefined step: "I am signed in as "admin@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:24:in `Given I am signed in as "admin@ticketee.com"'
    When I follow "TextMate 2"                   # features/step_definitions/web_steps.rb:56
    Then I should see the "Edit Proeject" link   # features/hidden_links.feature:26
      Undefined step: "I should see the "Edit Proeject" link" (Cucumber::Undefined)
      features/hidden_links.feature:26:in `Then I should see the "Edit Proeject" link'

  Scenario: Delete project link is hidden for non-signed-in users # features/hidden_links.feature:28
    Given I am on the homepage                                    # features/step_definitions/web_steps.rb:44
    When I follow "TextMate 2"                                    # features/step_definitions/web_steps.rb:56
      undefined method `admins_only' for #<#<Class:0x9c16994>:0xaa71660> (ActionView::Template::Error)
      ./app/views/projects/show.html.erb:3:in `_app_views_projects_show_html_erb___713092924_82350660'
      /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.12/lib/action_view/template.rb:145:in `block in render'
      ./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
      features/hidden_links.feature:30:in `When I follow "TextMate 2"'
    Then I should not see the "Delete Project" link               # features/hidden_links.feature:31
      Undefined step: "I should not see the "Delete Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:31:in `Then I should not see the "Delete Project" link'

  Scenario: Delete project link is hidden for signed-in users # features/hidden_links.feature:33
    Given I am signed in as "user@ticketee.com"               # features/hidden_links.feature:34
      Undefined step: "I am signed in as "user@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:34:in `Given I am signed in as "user@ticketee.com"'
    When I follow "TextMate 2"                                # features/step_definitions/web_steps.rb:56
    Then I should not see the "Delete Project" link           # features/hidden_links.feature:36
      Undefined step: "I should not see the "Delete Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:36:in `Then I should not see the "Delete Project" link'

  Scenario: Delete project link is shown to admins # features/hidden_links.feature:38
    Given I am signed in as "admin@ticketee.com"   # features/hidden_links.feature:39
      Undefined step: "I am signed in as "admin@ticketee.com"" (Cucumber::Undefined)
      features/hidden_links.feature:39:in `Given I am signed in as "admin@ticketee.com"'
    When I follow "TextMate 2"                     # features/step_definitions/web_steps.rb:56
    Then I should see the "Delete Project" link    # features/hidden_links.feature:41
      Undefined step: "I should see the "Delete Project" link" (Cucumber::Undefined)
      features/hidden_links.feature:41:in `Then I should see the "Delete Project" link'

Failing Scenarios:
cucumber features/hidden_links.feature:13 # Scenario: Edit project link is hidden for non-signed-in users
cucumber features/hidden_links.feature:28 # Scenario: Delete project link is hidden for non-signed-in users

6 scenarios (2 failed, 4 undefined)
30 steps (2 failed, 4 skipped, 10 undefined, 14 passed)
0m0.748s

You can implement step definitions for undefined steps with these snippets:

Then(/^I should not see the "(.*?)" link$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Given(/^I am signed in as "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Then(/^I should see the "(.*?)" link$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

и это show.html.erb, где у меня есть admins_only:

<% title(@project.name, "Projects") %>
<h2><%= @project.name %></h2>
<% admins_only do %>
<%= link_to "Edit Project", edit_project_path(@project) %>
<%= link_to "Delete Project", @project, :method => :delete, :confirm => "Are you sure you want to delete this project?" %>
<% end %>
<%= link_to "New Ticket", new_project_ticket_path(@project) %>
<ul id= 'tickets'>
    <% @project.tickets.each do |ticket| %>
    <li>
        #<%= ticket.id %> - <%= link_to ticket.title, [@project, ticket] %>
    </li>
    <% end %>
</ul>

это то, что я пытался, но не смог решить мою проблему, поэтому помощь оценили...

1 ответ

Решение

Вы не реализовали определения шагов в соответствующем файле _steps.rb. Web Steps предоставляет некоторые общие определения шагов, но они ужасны, и вы не должны их использовать.

Чтобы решить это, вам нужно вставить это:

Then(/^I should not see the "(.*?)" link$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Given(/^I am signed in as "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

Then(/^I should see the "(.*?)" link$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

в файл ruby ​​в каталоге step_definitions и замените ожидающий код кодом, чтобы выполнить условия шага.

Другие вопросы по тегам