首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cucumber异步javascript错误

cucumber异步javascript错误
EN

Stack Overflow用户
提问于 2012-10-04 12:05:26
回答 1查看 373关注 0票数 0

只要按下completed /index.html.erb中的“保存”按钮,就会完成一个ajax请求,并追加一行,显示同一页面中的Composition.content字符串。但我的黄瓜测试显示了一个错误:

代码语言:javascript
复制
Feature: User creates compositions

  Scenario: User creates a composition                                                   # features/composition.feature:2
    Given I go to the compositions page                                                  # features/step_definitions/composition_steps.rb:1
    And I fill in "Content" with "My Globe number: +63-916-475-4261"                     # features/step_definitions/composition_steps.rb:5
    When I press "Save"                                                                  # features/step_definitions/composition_steps.rb:9
    Then I should see "My Globe number: +63-916-475-4261" added in the compositions page # features/step_definitions/composition_steps.rb:18
      Unable to find xpath "/html" (Capybara::ElementNotFound)
      (eval):2:in `text'
      ./features/step_definitions/composition_steps.rb:22:in `/^I should see "(.*?)" added in the compositions page$/'
      features/composition.feature:6:in `Then I should see "My Globe number: +63-916-475-4261" added in the compositions page'

Failing Scenarios:
cucumber features/composition.feature:2 # Scenario: User creates a composition

1 scenario (1 failed)
4 steps (1 failed, 3 passed)
0m0.904s

错误是因为添加的行在那个点还没有被呈现吗?请给我一些答案。谢谢!

下面是涉及到的文件。

composition.feature

代码语言:javascript
复制
Feature: User creates compositions
  Scenario: User creates a composition
    Given I go to the compositions page
    And I fill in "Content" with "My Globe number: +63-916-475-4261"
    When I press "Save"
    Then I should see "My Globe number: +63-916-475-4261" added in the compositions page

composition_step.rb

代码语言:javascript
复制
Given /^I go to the compositions page$/ do
  visit compositions_path
end

Given /^I fill in "(.*?)" with "(.*?)"$/ do |arg1, arg2|
  fill_in(arg1, :with => arg2)
end

When /^I press "(.*?)"$/ do |arg1|
  click_button 'Save'
end

Then /^I should see "(.*?)" added in the compositions page$/ do |arg1|
  page.should have_content(arg1)
end

compositions/index.html.erb

代码语言:javascript
复制
<%= form_for @new_composition, remote: true do |f| %>
  <%= f.label :content %>
  <%= f.text_field :content %>
  <%= f.submit 'Save', id: "composition_submit" %>
<% end %>

<h1>Listing compositions</h1>

<table id="compositions_table">
<% @compositions.each do |composition| %>
  <tr>
    <td><%= composition.content %></td>
  </tr>
<% end %>
</table>

compositions_controller.rb

代码语言:javascript
复制
class CompositionsController < ApplicationController
  def index
    @new_composition = Composition.new
    @compositions = Composition.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @compositions }
    end
  end

  def create
    @composition = Composition.new(params[:composition])

    respond_to do |format|
      if @composition.save
        format.js
        format.json { render json: @composition, status: :created, location: @composition }
      else
        format.js
        format.json { render json: @composition.errors, status: :unprocessable_entity }
      end
    end
  end
end

create.js.erb

代码语言:javascript
复制
$('#compositions_table').prepend("<%= j(render('compositions/composition', composition: @composition)) %>")
EN

回答 1

Stack Overflow用户

发布于 2012-10-04 20:21:19

在composition_step.rb中试用

代码语言:javascript
复制
When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
 fill_in(field, :with => value)
end

When /^(?:|I )press "([^\"]*)"$/ do |button|
 click_button(button)
end

Then /^I should see "([^"]*)" within "([^"]*)"$/ do |text, selector|
  find(:xpath, "//#{selector}[contains(text(),'#{text}')]").should_not(be_nil, "Could  not find the text '#{text}' within the selector '#{selector}'")
end

Then /^I should see "(.*?)" added in the compositions page$/ do |text|
  page.should have_content(text)
end

像这样的场景

代码语言:javascript
复制
Scenario: User creates a composition
    Given I go to the compositions page
    When I fill in "Content" with "My Globe number: +63-916-475-4261"
    And I press "Save"
    Then I should see "My Globe number: +63-916-475-4261" added in the compositions page
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12720125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档