在黄瓜中调用rack :: test POST方法错误"未定义的方法`call'对于nil:NilClass(NoMethodError)"

时间:2015-03-13 05:18:50

标签: ruby-on-rails rspec cucumber capybara

我是Cucumber测试的新手,并探索测试POST到REST API的方法。

在提及http://anthonyeden.com/2013/07/10/testing-rest-apis-with-cucumber-and-rack.htmlHow to test posts in Rails / Capybara / Cucumber or Rspec时,我设法在我的Cucumber测试中继续发布网址,但是当我调用“post”时我遇到了以下错误     nil的未定义方法`call':NilClass(NoMethodError)

我的简化步骤如下

require "rack/test"
require "Capybara"

module CapybaraApp
  def app; Capybara.app; end
end
World(CapybaraApp)
World(Rack::Test::Methods)

When /^I send a POST request to "([^\"]*)" with:$/ do |url, body|
  header 'Accept', 'application/json'
  header 'Content-Type', 'application/json'
  post url, body
end

Then /^the JSON response should have (\d+) "([^\"]*)" elements$/ do |number_of_children, name|
  page = JSON.parse(last_response.body)
  page.map { |d| d[name] }.length.should == number_of_children.to_i
end

我的功能

Feature: Users API
  Background: Testing

  Scenario: Post Testing
    When I send a POST request to "/my_test_url/" with:
      """
      {
        "name":"Polly"
      }
      """    
    Then the JSON response should have 3 "user" elements

我的调试显示它是导致错误的步骤中的“帖子”。有人可以在这里启发我吗?谢谢!

更新:添加堆栈跟踪如下

      undefined method `call' for nil:NilClass (NoMethodError)
      /.rvm/gems/ruby-2.1.1/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
      /.rvm/gems/ruby-2.1.1/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
      /.rvm/gems/ruby-2.1.1/gems/rack-test-0.6.3/lib/rack/test.rb:67:in `post'
      /Documents/cucumberTest/steps_definition/test_step.rb:13:in `block in <top (required)>'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/core_ext/instance_exec.rb:48:in `instance_exec'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/core_ext/instance_exec.rb:48:in `block in cucumber_instance_exec'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/core_ext/instance_exec.rb:69:in `cucumber_run_with_backtrace_filtering'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/core_ext/instance_exec.rb:36:in `cucumber_instance_exec'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/rb_support/rb_step_definition.rb:97:in `invoke'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/step_match.rb:25:in `invoke'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/step_invocation.rb:60:in `invoke'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/step_invocation.rb:38:in `accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:106:in `block in visit_step'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:105:in `visit_step'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/step_collection.rb:19:in `block in accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/step_collection.rb:18:in `each'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/step_collection.rb:18:in `accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:100:in `block in visit_steps'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:99:in `visit_steps'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:15:in `block in execute'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime.rb:83:in `block (2 levels) in with_hooks'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime.rb:99:in `before_and_after'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime.rb:82:in `block in with_hooks'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime/support_code.rb:120:in `call'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime/support_code.rb:120:in `block (3 levels) in around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/language_support/language_methods.rb:9:in `block in around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/language_support/language_methods.rb:97:in `call'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/language_support/language_methods.rb:97:in `execute_around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/language_support/language_methods.rb:8:in `around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime/support_code.rb:119:in `block (2 levels) in around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime/support_code.rb:123:in `call'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime/support_code.rb:123:in `around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime.rb:94:in `around'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime.rb:81:in `with_hooks'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:13:in `execute'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/scenario.rb:32:in `block in accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/scenario.rb:79:in `with_visitor'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/scenario.rb:31:in `accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:58:in `block in visit_feature_element'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:57:in `visit_feature_element'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/feature.rb:38:in `block in accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/feature.rb:37:in `each'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/feature.rb:37:in `accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:27:in `block in visit_feature'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:26:in `visit_feature'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/features.rb:28:in `block in accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/features.rb:17:in `each'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/features.rb:17:in `each'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/features.rb:27:in `accept'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:21:in `block in visit_features'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/ast/tree_walker.rb:20:in `visit_features'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/runtime.rb:49:in `run!'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/lib/cucumber/cli/main.rb:47:in `execute!'
      /.rvm/gems/ruby-2.1.1/gems/cucumber-1.3.19/bin/cucumber:13:in `<top (required)>'
      /.rvm/gems/ruby-2.1.1/bin/cucumber:23:in `load'
      /.rvm/gems/ruby-2.1.1/bin/cucumber:23:in `<main>'
      /.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
      /.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'

0 个答案:

没有答案