undefined method `have_content' using Cucumber / Capybara / sinatra - ruby

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:invalidate_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:inThen the system will prompt me for a survey summary'

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.

Related

Unable to launch firefox browser and run test with RSpec in RubyMine

I am new to Ruby and Rspec. I just did below steps.
Clicked on New Project in RubyMine
Gave title as Selenium2(just some random name)
I am using ruby 2.7.1
Created a new directory 'src' under Selenium2 folder
Added test.rb file
I added below code in test.tb file(already did gem install for selenium-webdriver and rspec) as shown in below screenshot.
Right clicked on test.rb and selected Run 'test'.
Got the below output as shown in below screenshot. As a newbie I dont see any issue with the code. But its not even launching Firefox browser and not printing puts in the console.
Am I missing something here?
I think you're doing it wrong.
In your project root folder, add Gemfile file, this Gemfile can have something like this:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.5'
gem 'rspec', '~> 3.9'
gem 'capybara', '~> 3.30'
gem 'selenium-webdriver', '~> 3.142', '>= 3.142.6'
Create a spec/ directory in your project root
Create a log/ directory in your project root for your selinium logs.
Create a spec/spec_helper.rb file with configuration like this:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
Selenium::WebDriver.logger.level = :debug
Selenium::WebDriver.logger.output = File.dirname(Dir.pwd) + '/project_dir_name/log/selenium.log'
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.javascript_driver = :firefox
Capybara.app_host = 'http://127.0.0.1:3005'
Capybara.default_max_wait_time = 10
RSpec.configure do |config|
config.before(:each) do
config.include Capybara::DSL
end
end
The above code will setup rspec, capybara. You can change the driver, host and other configs if you want.
Now create a new spec/features/test.rb file with something like this:
require 'spec_helper'
describe 'Google homepage test', js: true do
before(:each) do
visit('https://google.com')
end
describe "First test" do
it "check title" do
expect(page.title).to be == "some text"
end
end
end
Then run: bundle exec rspec spec/features/test.rb from your project root.
If you don't want this way, then the problem is that you're running ruby test.rb, actually test.rb this should be runned via rspec:
$ rspec test.rb
If you're using bundler, then:
$ bundle exec rspec test.rb

uninitialized constant SitePrism from page declariation

I'm trying to set up a basic framework using Capybara, Cucumber and SitePrism, but I keep getting the error, "uninitialized constant SitePrism (NameError)" when I kick off a test.
Gemfile:
gem 'cucumber', '2.3.3'
gem 'capybara', '2.6.2'
gem 'selenium-webdriver', '2.53.0'
gem 'rspec'
gem 'site_prism'
gem 'mime-types', '>2.6', '<2.99.1'
Env.rb
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'site_prism'
require 'cucumber'
require_rel '../features/pages'
require_rel '../features/classes'
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Login page
class LoginPage < SitePrism::Page
end
Login class
class Login
def initialize
#current_page = LoginPage.new
end
The error is being thrown on the line with "class LoginPage < SitePrism::Page". RubyMine can't find the SitePrism declaration to go to either. Am I missing something in the setup?
Your error looks 'require' related, but here is how I got it to work via:
https://github.com/thuss/standalone-cucumber
Haven't used cucumber in a while, but the way I see "pages" currently implemented in my Rails project:
Create a file in "features/support/pages"
Follow the namespace conventions
Use modules, then import via the World() method.
Maybe this might work:
features/support/pages/login_page.rb
module Pages
module LoginPage
class LoginPageObj < SitePrism::Page
end
def login_obj
LoginPageObj.new
end
end
end
World(Pages::LoginPage)
Env file:
require 'capybara'
require 'capybara/cucumber'
require 'site_prism'
Capybara.configure do |config|
config.default_driver = :selenium
config.app_host = 'http://www.google.com'
end
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Notice how I didn't have to explicitly require any pages class, it looks like Cucumber might require it for you?
Note this is without RubyMine (I dont use it). If it works without RubyMine, I'd point fingers to that.
So the reason you were getting this problem is because of the files being auto-loaded. Just ensure you require the gem files first so the namespaces are understood

Ruby gem dependency loading

I'm putting some shared models for a rails app inside it's own gem. It's just models, so I'm not using an engine. Getting it set up seemed to work fine until I added the "acts_as_list" gem.
# gem - domain.gemspec
spec.add_dependency "acts_as_list"
# gem - lib/domain.rb
require "acts_as_list"
# gem - lib/domain/models/page.rb
acts_as_list scope: [:ancestry]
This works fine in the console for my gem, I can run methods specific to acts_as_list as usual. However, when I add my gem to another project, it gives me an error.
# project - Gemfile
gem "www_domain", path: "../www_domain"
Bundler::GemRequireError: There was an error while trying to load the gem 'domain'.
NoMethodError: undefined method `acts_as_list' for #<Class:0x0055da70121ab0>
/home/shaun/sites/demo/domain/lib/domain/models/page.rb:32:in `<class:Page>'
Is there something special I have to do in this case to access the acts_as_list method because my model is inside a gem?
Update: Here is my complete lib/domain.rb file for the gem:
require "yaml"
require "active_record"
require "acts_as_list"
require "ancestry"
# rbfiles = File.join(File.dirname(__FILE__), "lib", "**", "*.rb")
# Dir.glob(rbfiles).each do |file|
# require file.gsub("lib/", "")
# end
module Domain
# Your code goes here...
def self.root
File.dirname(__dir__)
end
def self.db_config
YAML.load_file("#{Domain.root}/db/config.yml")["development"]
end
end
require "domain/version"
require "domain/models/page"
require "domain/models/menu"
require "domain/models/article"
require "domain/models/page_part"
I can use acts_as_list and ancestry methods in the console of my gem (running bin/console from the gem directory). But the project console (running bundle exec rails console from the project directory) will not start because of the gem error I mentioned.
This might result from a load order issue if the model being required before the require :acts_as_list statement. Check to see if the gem specifies the load order. If not, you could try something like:
# gem - lib/domain.rb
require "acts_as_list"
require "models/page"
If the load order is unclear, I find it helpful to simply add a
puts __FILE__
at the top of the relevant source files.

Why is bundler loading all my gems instead of specific ones

I have a Sinatra application and I'm trying to use groups in my Gemfile in order to load only specified gems. But, when I restrict loading to just one group bundler still loads every gem in the file. Here's my Gemfile:
source 'https://rubygems.org'
group :one do
gem 'sinatra'
end
group :two do
gem 'bitly'
end
And here's my application:
require 'bundler/setup'
Bundler.require(:one)
class App < Sinatra::Base
configure do
puts Gem.loaded_specs.keys.sort.join("\t")
end
get '/foo' do
end
end
And I can clearly see the Bitly gem loaded when the application starts. What am I doing wrong?
Use
require 'bundler'
instead of
require 'bundler/setup'
The last one automatically loads all gems in Gemfile: Why do you need "require 'bundler/setup'"?

Using Rspec with Rails and Neo4j.rb: missing method

Please help,
I am doing a project with rails and neo4j with the neo4j.rb gem by ronge. I can get generator and CRUD working with Neo4j. But, everytime I run 'rspec' tests, there is method missing error within the configure block of RSpec within spec_helper.
Can anyone helps me figure this out?
Thanks so much!!
Rails and JRuby version.
saasbook#saasbook:~/temp$ rails -v
Rails 3.2.17
saasbook#saasbook:~/temp$ ruby -v
jruby 1.7.10 (1.9.3p392) 2014-01-09 c4ecd6b on Java HotSpot(TM) Client VM 1.7.0_51-b13 [linux-i386]
Create Rails App
rails new myapp -m http://andreasronge.github.com/neo4j/rails.rb -O -T
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.17'
gem 'jruby-openssl'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'therubyrhino'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :development, :test do
gem "rspec-rails"
end
gem "neo4j"
Then,
saasbook#saasbook:~/test/myapp$ rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
saasbook#saasbook:~/test/myapp$ rails g model testnode
invoke neo4j
create app/models/testnode.rb
invoke rspec
create spec/models/testnode_spec.rb
When I run rspec, here is the error:
saasbook#saasbook:~/test/myapp$ rspec
NoMethodError: undefined method `fixture_path=' for #<RSpec::Core::Configuration:0xbcf6bf>
(root) at /home/saasbook/test/myapp/spec/spec_helper.rb:21
configure at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core.rb:120
(root) at /home/saasbook/test/myapp/spec/spec_helper.rb:11
require at org/jruby/RubyKernel.java:1083
(root) at /home/saasbook/test/myapp/spec/models/testnode_spec.rb:1
load at org/jruby/RubyKernel.java:1099
(root) at /home/saasbook/test/myapp/spec/models/testnode_spec.rb:1
each at org/jruby/RubyArray.java:1613
(root) at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:1
load_spec_files at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896
load_spec_files at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896
run at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22
Also, here is the generated spec_helper.rb file:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
Resolved, see comments on the questions.
:fixture_path and :use_transactional_fixtures are two settings added by rspec-rails ONLY under the presence of ActiveRecord. So of course they are method missing. Just comment them out, you don't need them if you use Neo4j.rb

Resources