undefined method `expect' for #<Spinach::Features - ruby-on-rails

I've followed the README at the spinach repo as well as including spinach-rails instead of the base spinach gem.
Spinach documentation states:
# When using Capybara, you can switch the driver to use another one with
# javascript capabilities (Selenium, Poltergeist, capybara-webkit, ...)
#
# Spinach already integrates with Capybara if you add
# `require spinach/capybara` in `features/support/env.rb`.
Which I have done:
Head of my env.rb file:
require 'spinach-rails'
require 'spinach/capybara'
ENV['RAILS_ENV'] = 'feature'
require 'rspec'
require 'factory_girl_rails'
require 'webmock'
require 'coffee_script'
include WebMock::API
WebMock.disable_net_connect!(:allow_localhost => true)
require_relative '../../config/environment'
The step failing in the test reads:
step 'I see the Generals Page' do
expect(page).to have_content("Inventory")
end
When I inspect the Spinach::FeatureSteps class using RubyMine I can see two hits - one to the "base" class and the other to the capybara version:
require 'capybara'
require 'capybara/dsl'
require 'rbconfig'
require 'spinach/config'
require_relative 'feature_steps'
module Spinach
class FeatureSteps
# Spinach's capybara module makes Capybara DSL available in all features.
#
# #example
# require 'spinach/capybara'
# class CapybaraFeature < Spinach::FeatureSteps
# Given "I go to the home page" do
# visit '/'
# end
# end
#
module Capybara
include ::Capybara::DSL
Which really makes it seem like there should be a switch of sorts in the spinach configuration that lets me choose to use the capybara DSL in my tests.
But it doesn't appear to be working properly.
How can I get Capybara working with Spinach?
Gemfile.lock:
spinach (0.8.10)
colorize
gherkin-ruby (>= 0.3.2)
json
spinach-rails (0.2.1)
capybara (>= 2.0.0)
railties (>= 3)
spinach (>= 0.4)
test and feature group in Gemfile:
group :test, :feature do
gem "factory_girl_rails"
gem "capybara"
gem "webmock"
gem "capybara-webkit"
gem "spinach-rails"
end

Related

Minitest console output does not show colors

I installed ansicon to make ansi colorized console output possible for minitest test feedback on windows.
I am running minitest with the minitest-reporters gem to format the output, yet whatever I do I can't get the output to show colors (all text is black).
Here's my test_helper.rb file:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new( { :color => true } )
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
Does anyone know this problem?
Reference: https://github.com/kern/minitest-reporters
I see the post is from 2015. 4 years passed since then.
Anyway, let me share how it worked for me yesterday.
My environment:
Ubuntu 18.04
rails 6.0.1
ruby 2.6.5
1) Gemfile:
gem 'minitest-reporters'
2) bundle install
3) test_helper.rb:
require "minitest/reporters"
Minitest::Reporters.use!
4) rake

Confused - spec_helper.rb:94:in `<top (required)>': uninitialized constant Shoulda (NameError)

I am working on a project with Rails 4 & Mongoid 4. I am trying to set up Shoulda-matchers (version 2.8.0), following thoughtbot/shoulda-matchers, which points to another README called README for 2.8.0. And I am hoping to use the mongoid-rspec for testing.
However I keep getting spec_helper.rb:94:in '<top (required)>': uninitialized constant Shoulda (NameError)
I added this in Gemfile: following thoughtbot/shoulda-matchers
group :test do
gem 'shoulda-matchers'
end
I also added in spec_helper.rb (Which is where the error comes from) - following thoughtbot/shoulda-matchers
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
I have tried googling but there is no direct solution to this (Or there is no direct problem to this). I added require: false in the Gemfile, following README for 2.8.0
group :test do
gem 'shoulda-matchers', require: false
end
I added require 'shoulda/matchers' in the rails_helper.rb. My order of 'requires' is this:
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'rspec/rails' is below require 'spec_helper' by default. And by the README provided on the github page, I should place shoulda\matcher below 'rspec/rails'. I have also tried to place require 'shoulda/matchers' on top of require 'spec_helper' but it didn't work.
My versions:
Rails 4.2.1
Ruby 2.2.1
Mongoid ~ 4.0.0
rspec-rails ~ 3.0
mongoid-rspec ~ 2.1.0
shoulda-matchers 2.8.0
I really appreciate any help.
From the link you provided:
NOTE: The new configuration syntax isn't available in a public release just yet -- please refer to the README for 2.8.0 for the current installation instructions.
That note is from the master branch. The current release (2.8.0) has a different set of documentation. Confusing, I know.
Just remove that configuration section from spec/spec_helper.rb and all should be rainbows and unicorns once again.
Just insert these lines into spec/spec_helper.rb:
config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)
Reference:
https://github.com/thoughtbot/shoulda-matchers#availability-of-matchers-in-various-example-groups

Rspec NoMethodError: undefined method `errors_on'

I'm testing for validation of entry of a city attribute in an addresses model. My model spec is as follows
require 'rails_helper'
RSpec.describe Address, :type => :model do
before(:each) do
#valid_attributes = {
street: 'rock avenue',
city: 'MSA',
zip: '00100',
person_id: 1
} # runs before each it block
end
context "Validations" do
it 'must have a city' do
a = Address.new
expect(a.errors_on(:city)).not_to be_empty
end
end
end
When I run the spec, i get the following error
1) Address Validations must have a city
Failure/Error: expect(a.errors_on(:city)).not_to be_empty
NoMethodError:
undefined method `errors_on' for #<Address:0xd844538>
# ./spec/models/address_spec.rb:19:in `block (3 levels) in <top (required)>'
My test gems are set up as following in my Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails', :require => false
end
group :test do
gem 'capybara'
gem 'guard'
gem 'guard-rspec'
gem 'libnotify'
gem 'rspec-collection_matchers'
end
I have included the rspec collection matchers in my spec_helper, so I don't understand why I am getting this error
My spec_helper.rb
require 'capybara/rspec'
require 'factory_girl_rails'
require 'rspec/collection_matchers'
However I can see that the method, errors_on has been defined in rspec-collection_matchers gem as shown here
My .rspec
--color
--require spec_helper
I have also changed the position of
require 'rspec/collection_matchers'
to rails_helper.rb, but running the test suite again brings up the same error
rails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'rspec/collection_matchers'
require 'capybara/rspec'
require 'factory_girl_rails'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
My spec_helper.rb is empty with only the block below:
RSpec.configure do |config|
end
I am using Rails 4.1.0, rspec 3.0.0 and rspec-collection_matchers 1.0.0
Without seeing the repo, this is only a guess based on the information. I think this is a load order problem.
It looks like you are loading rspec/collection_matchers in spec_helper, not rails_helper. If you are using the new default configuration, --require spec_helper is in your .rspec file. When you run rspec this will require spec_helper very early on. When that happens it requires rspec/collection_matchers. However, at that point rails_helper has not been loaded. Unfortunately, this also means most of ActiveSupport including ActiveModel are not loaded yet. It also means the Rails autoloading functionality has not been loaded, so those won't get defined when you reference them.
All this to say, the check for ActiveModel here returns false. Which does not load errors_on.
Try moving the require into rails_helper.
Note on testing in isolation:
If you are going for faster testing in isolation this is my suggestion:
spec_helper should be kept very minimal and lightweight, that means for the most part you shouldn't add any require declarations to it
Move the requires you listed from spec_helper into rails_helper, those are mostly Rails specific and do not belong in spec_helper as Rails is not loaded there
If you need access to the other rspec/collection_matchers functionality in specs which do not require Rails, then just add the require 'rspec/collection_matchers' to the top of the spec file; Ruby will handle making sure it is only loaded once

my tests don't run with minitest

I'm totally new in testing and after two years of rails development, it has to change!
Therefore, I'm trying to install minitest on a rails 4 application.
I did the following:
updated my gemfile:
group :test do
gem 'factory_girl'
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'minitest-colorize'
gem 'minitest-focus'
gem "shoulda-matchers"
gem "guard-rspec"
end
created a helper in test/test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
#require "minitest/rails"
require "minitest/spec"
require 'minitest/focus'
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
require "minitest/rails/capybara"
# Uncomment for awesome colorful output
require "minitest/pride"
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
ActiveRecord::Migration.check_pending!
fixtures :all
extend MiniTest::Spec::DSL
#use ActiveSupport::TestCase when describing an ActiveRecord model
register_spec_type self do |desc|
desc < ActiveRecord::Base if desc.is_a? Class
end
# Add more helper methods to be used by all tests here...
def self.prepare
# Add code that needs to be executed before test suite start
end
prepare
def setup
# Add code that need to be executed before each test
end
def teardown
# Add code that need to be executed after each test
end
end
generated a scaffold to have some "test" testfiles, like this one:
models/project_test.rb
require "test_helper"
describe Project do
before do
#project = Project.new
end
it "must be valid" do
#project.valid?.must_equal true
end
end
when I type rake minitest:models in terminal, this is what I get:
Run options: --seed 40421
# Running tests:
Fabulous tests in 0.049207s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Apparently, minitest runs but my tests are not executed. What's wrong?
Thanks!!!
The issue was due to a conflict with rspec which was added as a dependency by guard-rspec.
many thanks to #blowmage!

How to run single test class using minitest in ROR

My Gemfile looks like this:-
group :test do
# Pretty printed test output
gem 'capybara'#,'1.1.2'
gem 'cucumber-rails','1.2.1'
gem 'cucumber','1.1.4'
gem 'rspec-rails','2.8.1'
gem 'rspec-cells','0.1.2'
gem "factory_girl_rails"
gem "guard-rspec"
gem "minitest"
gem 'headless'
gem 'minitest-rails'
gem 'minitest-rails-capybara'
end
minitest_helper.rb looks like :-
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
class ControllerTest < MiniTest::Spec
include Rails.application.routes.url_helpers
include Capybara::DSL
register_spec_type(/integration$/, self)
end
And my products_controller_test.rb looks like this:-
require "minitest_helper"
describe "Products Controller" do
it "shows product's name" do
uname="Glasses"
product1 = Product.create!(:name => uname, :description => uname, :no_of_items => 3,:fee_percentage => 4)
visit products_path
page.text.must_include "Glasses"
end
end
BUT..after executing ruby -Itest test/controllers/products_controller_test.rb
I get no error,no indication to show that this test class has been loaded :-
ruby -Itest test/controllers/products_controller_test.rb
:public is no longer used to avoid overloading Module#public, use :public_folder instead
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/resque-1.19.0/lib/resque/server.rb:12:in `<class:Server>'
Loaded suite test/controllers/products_controller_test
Started
Finished in 0.004953 seconds.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
its first time i am using Minitest...
Your Gemfile is a little heavy... If you remove all the RSpec references, you'll run just fine.
(The "describe" and "it" methods are being usurped by rspec)
Remove:
gem 'rspec-rails','2.8.1'
gem 'rspec-cells','0.1.2'

Resources