未定义的方法“have_content”使用黄瓜/水豚/sinatra

发布于 2025-01-04 00:42:26 字数 1910 浏览 1 评论 0原文

我正在尝试验证 Sinatra 应用程序初始页面的开始,但正在努力让测试框架正常工作。谷歌搜索建议我添加 cucumber/rails/rspec 或类似的,但是在不使用 Rails 时我真的需要添加与 Rails 相关的库吗?

这是我的 Gemfile

source :rubygems

gem 'sinatra', '1.3.1'
gem 'json', '1.6.3'


group :test do
  gem 'rspec', '2.7.0'
  gem 'cucumber', '1.1.3'
  gem 'capybara', '1.1.2'
  gem 'rack-test', '0.6.1'
end

和我的步骤:

Given /^a room needs to be surveyed$/ do

end

When /^I start a survey$/ do
  survey_tool.start_a_survey
end

Then /^the system will prompt me for a survey summary$/ do
  survey_tool.validate_start_a_survey_page
end

以及我的世界扩展

module KnowsTheUserInterface
  class UserInterface
    include Capybara::DSL

    def start_a_survey()
      visit '/start_a_survey'
    end

    def validate_start_a_survey_page ()
      page.should have_content('Welcome')
    end
  end
  def survey_tool
     @survey_tool ||=UserInterface.new
  end

end

World(KnowsTheUserInterface)

和我的环境

require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'survey_tool')

require 'capybara/cucumber'
require 'capybara/rspec'
Capybara.app = Sinatra::Application
Sinatra::Application.set :environment, :test

我收到的错误是

场景:开始调查 # features/start_a_survey.feature:4 给定一个房间需要进行调查 # features/step_definitions/start_a_survey_steps.rb:1 当我开始调查时 # features/step_definitions/start_a_survey_steps.rb:5 然后系统会提示我输入调查摘要# features/step_definitions/start_a_survey_steps.rb:9 #的未定义方法 have_content' (无方法错误) ./features/support/world_extensions.rb:10:invalidate_start_a_survey_page' ./features/step_definitions/start_a_survey_steps.rb:10:in /^系统将提示我输入调查摘要$/' features/start_a_survey.feature:7:in然后系统会提示我输入调查摘要'

I'm trying to validate the start of an initial page for a Sinatra application but am struggling to get the testing framework working. Googling around suggests I add cucumber/rails/rspec or similar, but do I really need to add rails related libraries when not using Rails?

Here's my Gemfile

source :rubygems

gem 'sinatra', '1.3.1'
gem 'json', '1.6.3'


group :test do
  gem 'rspec', '2.7.0'
  gem 'cucumber', '1.1.3'
  gem 'capybara', '1.1.2'
  gem 'rack-test', '0.6.1'
end

and my steps:

Given /^a room needs to be surveyed$/ do

end

When /^I start a survey$/ do
  survey_tool.start_a_survey
end

Then /^the system will prompt me for a survey summary$/ do
  survey_tool.validate_start_a_survey_page
end

and my world extensions

module KnowsTheUserInterface
  class UserInterface
    include Capybara::DSL

    def start_a_survey()
      visit '/start_a_survey'
    end

    def validate_start_a_survey_page ()
      page.should have_content('Welcome')
    end
  end
  def survey_tool
     @survey_tool ||=UserInterface.new
  end

end

World(KnowsTheUserInterface)

and my env

require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'survey_tool')

require 'capybara/cucumber'
require 'capybara/rspec'
Capybara.app = Sinatra::Application
Sinatra::Application.set :environment, :test

The error I receive is

Scenario: Start a survey # features/start_a_survey.feature:4
Given a room needs to be surveyed # features/step_definitions/start_a_survey_steps.rb:1
When I start a survey # features/step_definitions/start_a_survey_steps.rb:5
Then the system will prompt me for a survey summary # features/step_definitions/start_a_survey_steps.rb:9
undefined method have_content' for #<KnowsTheUserInterface::UserInterface:0x007f910465fc38> (NoMethodError)
./features/support/world_extensions.rb:10:in
validate_start_a_survey_page'
./features/step_definitions/start_a_survey_steps.rb:10:in /^the system will prompt me for a survey summary$/'
features/start_a_survey.feature:7:in
Then the system will prompt me for a survey summary'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2025-01-11 00:42:27

我认为这里的问题是您没有包含 rspec 期望。 Capybara 节点有一个 has_content? 方法。

rspec 期望,当给定“have_content”作为参数时,调用“has_content?”在目标上。

因此,请尝试将 'rspec-expectations' 添加到您的 Gemfile 中,并将 require 'rspec/expectations' 添加到您的模块中。

I think the problem here is that you do not have the rspec expectations included. A Capybara node has a has_content? method.

It is the rspec expectation that, when given 'have_content' as an argument, calls 'has_content?' on the target.

So try adding 'rspec-expectations' to your Gemfile, and adding require 'rspec/expectations' into your module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文