Guard + Spork slowing down tests, not receiving --drb option - ruby

I am developing a Rails project via a Vagrant box (Ubuntu 32-bit, Rails 4.0.3, Ruby 2.1.0p0).
I've just tried adding Spork to my project to speed up my tests (using RSpec, Capybara), and am seeing significantly slower tests. If I run simply "rspec .", all my tests pass in 5.83 seconds. However, when I run guard via "guard -c -p", I save one of my spec files, and I get a time of 26.08 seconds.
Note: I have to run "guard -p" to actually get guard to run my tests on file save through Vagrant.
When guard starts running the tests, it shows the args:
["--color", "--failure-exit-code", "2", "--format", "progress", "--format",
"Guard::RSpec::Formatter", "--require", "/home/vagrant/.rvm/gems/ruby-2.1.0/
gems/guard-rspec-4.2.7/lib/guard/rspec/formatter.rb", "spec"]...
I see that "--format" is listed twice, and "--drb" is not showing up at all.
Here's my Guardfile:
guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' }, :test_unit => false do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
end
guard :rspec, :cmd => 'rspec --drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
end
guard 'livereload' do
watch(%r{app/views/.+\.(erb|haml|slim)$})
watch(%r{app/helpers/.+\.rb})
watch(%r{public/.+\.(css|js|html)})
watch(%r{config/locales/.+\.yml})
# Rails Assets Pipeline
watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
end
Here is my spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# 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'
require 'capybara/rspec'
# 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 }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
#Devise
config.include Devise::TestHelpers, type: :controller
#Capybara
config.include Capybara::DSL
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
The only thing in my .rspec :
--color
The relevant part of my Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 2.0'
gem 'factory_girl_rails'
gem 'guard-rspec'
gem 'guard-livereload'
gem 'spork-rails'
gem 'guard-spork'
end
group :test do
gem 'shoulda-matchers'
gem 'capybara'
end
I've noticed that if I have guard running, then save a file, at times I will get an error:
Could not start Spork server for rspec after 30 seconds. I will continue
waiting for a further 60 seconds.
I'm not sure why it's taking so long after installing Spork, especially when rspec is much faster going through the same Vagrant box. Any ideas?

There's a new --listen-on option since Guard 2.5: https://github.com/guard/guard/releases/tag/v2.5.0
From Guard's README:
Use Listen's network functionality to receive file change events from the network. This is most useful for virtual machines (e.g. Vagrant) which have problems firing native filesystem events on the guest OS.
Suggested use:
On the host OS, you need to listen to filesystem events and forward them to your VM using the listen script:
$ listen -f 127.0.0.1:4000
Remember to configure your VM to forward the appropriate ports, e.g. in Vagrantfile:
config.vm.network :forwarded_port, guest: 4000, host: 4000
Then, on your guest OS, listen to the network events but ensure you specify the host path
$ bundle exec guard -o '10.0.2.2:4000' -w '/projects/myproject'

Related

Issue with Selenium WebDrive with Docker image and Cucumber.io

Im trying to follow along with the following tutorial.
https://www.youtube.com/watch?v=cPF3GKkBHHY
I was getting the following error.
sh: 1: /sbin/ip: not found
But I managed to resolve that doing apt update then apt install iproute2 -y.
I am now getting the following error
cucumber
Feature: Search for things on Google and see results.
Scenario: See related words when searching. # features/basic.feature:3
When I search for "puppies" # features/step_defs.rb:1
Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError)
./features/step_defs.rb:2:in `"I search for {string}"'
features/basic.feature:4:in `I search for "puppies"'
Then I should see "dog" # features/step_defs.rb:7
The error im looking to resolve is this one.
Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError)
this is my .env.rb file.
require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'
#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
Capybara.register_driver :remote_chrome do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote,
:desired_capabilities => :chrome,
:url => "http://#{docker_ip}:4444/wd/hub",
:options => chrome_options)
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :remote_chrome
config.app_host = 'http://www.google.com' # change this to point to your application
end
Any help with this would be greatly appreciated.
Thanks!
You need to create an instance of Selenium::WebDriver::Chrome::Options and pass it to the options argument. Add this chrome_options = Selenium::WebDriver::Chrome::Options.new in your code like below.
require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'
#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
# Add options for the Chrome browser
chrome_options = Selenium::WebDriver::Chrome::Options.new
# Disable notifications
chrome_options.add_argument("--disable-notifications")
Capybara.register_driver :remote_chrome do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote,
:browser_name => :chrome,
:url => "http://#{docker_ip}:4444/wd/hub",
:options => chrome_options)
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :remote_chrome
config.app_host = 'http://www.google.com' # change this to point to your application
end
Solution:
Update env.rb file.
Before:
require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'
#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
Capybara.register_driver :remote_chrome do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote,
:desired_capabilities => :chrome,
:url => "http://#{docker_ip}:4444/wd/hub")
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :remote_chrome
config.app_host = 'http://www.google.com' # change this to point to your application
end
After:
require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'cucumber'
require 'pry'
require "selenium-webdriver"
# Ask capybara to register a driver called 'selenium'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(
app,
#what browser do we want? Must match whatever is in our seleniarm stand-alone image
browser: :firefox,
#where does it live? By passing a URL we tell capybara to use a selenium grid instance (not local)
url: "http://#{ENV['SELENIUM_HOST']}:#{ENV['SELENIUM_PORT']}"
)
end
# make the driver we just registered our default driver
Capybara.default_driver = :selenium
# set the default URL for our tests
Capybara.app_host = "https://www.google.com/"
Update basic.feature
Before:
Feature: Search for things on Google and see results.
Scenario: See related words when searching.
When I search for "puppies"
Then I should see "dog"
Scenario: Don't see unrelated words when searching.
When I search for "dachshund"
Then I should NOT see "fish"
After:
Feature: Search for things on Google and see results.
Background:
Given I accept cookies
Scenario: See related words when searching.
When I search for "puppies"
Then I should see "dog"
Scenario: Don't see unrelated words when searching.
When I search for "dachshund"
Then I should NOT see "fish"
Update step_defs.rb
Before:
When("I search for {string}") do |string|
visit "/"
fill_in "q", with: string
click_on "Google Search", match: :first
end
Then("I should see {string}") do |string|
page.should have_content(string)
end
Then("I should NOT see {string}") do |string|
page.should_not have_content(string)
end
After:
Given('I accept cookies') do
visit "/"
click_on "Accept all"
end
When("I search for {string}") do |string|
visit "/"
fill_in "q", with: string
click_on "Google Search", match: :first
end
Then("I should see {string}") do |string|
page.should have_content(string)
end
Then("I should NOT see {string}") do |string|
page.should_not have_content(string)
end
# a handy debugging step you can use in your scenarios to invoke 'pry' mid-scenario and poke around
When('I debug') do
binding.pry
end
Update Dockerfile
Before:
#start with the base ruby image
FROM ruby
#make sure we have a folder called /app
RUN mkdir /app
#cd into our app folder each time we start up
WORKDIR /app
#copy our Gemfile and Gemfile.lock
COPY Gemfile* /app/
#install the gems
RUN bundle
CMD cucumber
After:
#start with the base ruby image
FROM ruby
#always a good idea to update and have an editor
RUN apt-get update; apt-get install -y vim
#make sure we have a folder called /app
RUN mkdir /app
#cd into our app folder each time we start up
WORKDIR /app
#copy our Gemfile and Gemfile.lock
COPY Gemfile* /app/
#install the gems
RUN bundle
CMD cucumber
Update Gemfile
Before:
source 'https://rubygems.org'
gem 'cucumber'
gem 'capybara'
#gem 'capybara-cucumber'
gem 'capybara-screenshot'
gem 'pry'
gem 'rspec'
gem 'selenium-webdriver'
After:
source 'https://rubygems.org'
gem 'cucumber'
gem 'capybara'
gem 'capybara-screenshot'
gem 'pry'
gem 'rspec'
#gem 'selenium-webdriver', '3.142.7' # Haven't been able to make 4.x work, yet...
gem 'selenium-webdriver', '~> 4.4.0'
Update docker-compose.yml
Before:
version: '3'
services:
browser:
# See inside via VNC with the "debug" version
image: selenium/standalone-chrome-debug
# Slightly faster headless version
#image: selenium/standalone-chrome
ports:
- "5900:5900" #for VNC access
- "4444:4444" #for webdriver access
ruby:
build: .
volumes:
- .:/app
depends_on:
- browser
After:
version: '3'
services:
browser:
#use for Apple Silicon
#image: seleniarm/standalone-chromium
# Chrome is crashing for me so I'm using firefox for now
image: seleniarm/standalone-firefox
ports:
- "5900:5900" #for VNC access
- "4444:4444" #for webdriver access
- "7900:7900" #for web VNC access
ruby:
build: .
volumes:
- .:/app
depends_on:
- browser
environment:
- SELENIUM_HOST=browser
- SELENIUM_PORT=4444

Ruby: Strange string comparison assertion behaviour

Can anyone explain what is happening here? I have this simple class with some static methods, and I want to test them.
yaqueline/build/converters/asciidocconverter.rb
# encoding: UTF-8
require 'asciidoctor'
module Yaqueline
module Build
module Converters
class AsciiDocConverter < Converter
class << self
def matches path
path =~ /\.(asciidoc|adoc|ascii|ad)$/
end
def convert content
html = Asciidoctor.convert content, to_file: false, safe: :safe
html = get_guts_out_of_body html
puts "asciidoc #{html}"
html
end
def get_guts_out_of_body html
if html =~ /<body>/
puts "get guts: #{html}"
return html.match(%r{(?<=<body>).*(?=</body>)})
end
html
end
end # class << self
end # class
end
end
end
and the test in test/build/converters/asciidocconverter_test.rb:
# encoding: utf-8
require 'helper'
require 'yaqueline/build/converters/asciidocconverter'
class TestAsciidocConverter < Test::Unit::TestCase
should "be able to get body html from a document" do
value = %q{SUCCESS}
html = %Q{
<html>
<head>
<title>Hej värld</title>
</head>
<body>#{value}</body>
</html>}
guts = Yaqueline::Build::Converters::AsciiDocConverter.get_guts_out_of_body html
puts "guts was '#{guts}'"
assert value.eql?(guts), "guts was '#{guts}', expected '#{value}'"
end
end
When running the test with
$ rake test TEST=test/build/converters/asciidocconverter_test.rb
The results looks good to me:
Started
get guts:
<html>
<head>
<title>Hej värld</title>
</head>
<body>SUCCESS</body>
</html>
guts was 'SUCCESS'
F
===============================================================================================================================================================================
Failure:
guts was 'SUCCESS', expected 'SUCCESS'.
<false> is not true.
test: AsciidocConverter should be able to get body html from a document. (TestAsciidocConverter)
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:37:in `block in <class:TestAsciidocConverter>'
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:39:in `instance_exec'
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:39:in `block in create_test_from_should_hash'
===============================================================================================================================================================================
but the assertion fails which seems odd to me and I'll need some help.
I'm running ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
and my Gemfilelooks like
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"
gem 'mercenary'
gem 'safe_yaml'
gem 'kramdown'
gem 'colorator'
gem 'pathutil'
gem 'nokogiri'
gem 'sass'
gem 'listen', '~> 3.0'
gem 'asciidoctor'
gem 'tilt'
gem 'erubis'
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "rdoc", "~> 3.12"
gem "bundler", "~> 1.0"
gem "juwelier", "~> 2.1.0"
gem "simplecov", ">= 0"
gem 'rubocop', '~> 0.48.1', require: false
gem 'thin' # or whatever I end up with
gem 'minitest'
gem 'test-unit'
gem 'shoulda'
end
Maybe this helps to realize hat test harness I'm using.
Can anyone see the mistake or explain what's going on?
Cheers
Inspect the types of values being compared. One of them is not a string. (Thus, it can't be equal to a string).
guts = html.match(%r{(?<=<body>).*(?=</body>)})
guts # => #<MatchData "SUCCESS">
guts.to_s # => "SUCCESS"

Guard Silently Exits When Using WDM Gem

Whenever I try to use wdm in conjunction with guard on Windows, guard silently exits. However, if I force polling, then guard works correctly. Probably the best way to illustrate this is with an example. This is what happens when I run guard with wdm:
C:/path/to/code>bundle exec guard init rspec
11:47:02 - INFO - rspec guard added to Guardfile, feel free to edit it
C:/path/to/code>bundle exec guard
11:48:06 - INFO - Guard is using TerminalTitle to send notifications.
11:48:06 - INFO - Guard::RSpec is running
11:48:06 - INFO - Guard is now watching at 'C:/path/to/code'
C:/path/to/code>
However, if I force guard to poll for changes, then it behaves correctly by ending up at a guard console:
C:/path/to/code>bundle exec guard -p
11:50:03 - INFO - Guard is using TerminalTitle to send notifications.
11:50:03 - INFO - Guard::RSpec is running
11:50:03 - INFO - Guard is now watching at 'C:/path/to/code'
[1] guard(main)>
I'm hoping that someone might have some light to shed on this problem. I have tried searching around quite a bit, but have not been able to find any answers to this question.
Relevant Excerpts from my Gemfile:
source 'http://rubygems.org'
ruby '1.9.3'
require "rbconfig"
gem 'rails', '3.2.13'
gem "figaro", ">= 0.5.3"
gem "rspec-rails", ">= 2.12.2", :group => [:development, :test]
gem "factory_girl_rails", ">= 4.2.0", :group => [:development, :test]
group :test do
gem "capybara", ">= 2.0.2"
gem "database_cleaner", ">= 0.9.1"
gem "email_spec", ">= 1.4.0"
gem "guard-rspec"
gem "wdm", ">= 0.1.0" if RbConfig::CONFIG["target_os"] =~ /mswin|mingw|cygwin/i
end
My Guardfile:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
I don't have an answer to this, but I thought I might collect some of the issues that relate to it.
The primary issue is Problems on Windows in the listen gem.
There is a related issue on guard is Guard 2.0.3 daemon doesn't stay running on windows which was closed in favour of the listen issue.
The wdm gem also has an open issue: fail with listen gem.
Sorry I can't solve it, but at least there are on-going issues for this problem. The few people that work on Ruby for Windows are such gems! I apologise for the pun :)

prepare called on a closed database rails rspec

I am trying to integrate BDD in my rails app via rspec. I am using guard and spork-rails to speed the monitoring process up. I am getting this error:
An error occurred in an after hook
ActiveRecord::StatementInvalid: ArgumentError: prepare called on a closed database:
rollback transaction occurred at /Users/davidhahn/.rvm/gems/ruby-1.9.3-p286/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in initialize
I ran rake db:test:prepare and it ran without any errors. Since I'm using sqlite I checked to make sure that the user_man_test.sqlite file was in db/. My test is just a simple integration test:
require 'spec_helper'
describe "Authentication" do
describe "Login Page" do
it "should have the h1 'Welcome to User Management'" do
visit '/log_in'
page.should have_selector('h1', text: 'Welcome to User Management')
end
end
describe "Login" do
before { visit '/log_in' }
describe "with invalid information" do
before { click_button "Login" }
it { should have_selector('h1', text: 'Welcome to User Management') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
end
end
end
My spec_helper.rb looks like:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
~
Thanks for the help
Instead of spending hours trying to configure spork properly, I would advise you to look at Zeus. I'm sorry if this doesn't exactly answer your question, but I spent almost one year with spork, had tons of trouble configuring every time that I added a new test gem, and when I made the switch, everything magically worked (and in my experience, Zeus' performance is much better than Spork).

RSpec test fails on Travis-CI but on local machine pass successfully

I'm write some specs to cover my HTML helpers
describe Sinatra::Helpers::HTML do
describe 'tag' do
it 'should retun selfclosed tag' do
Helpers.tag(:br, {}, true).should == '<br />'
end
it 'should have valid attributes' do
Helpers.tag(:div, :class => 'test').should include("class='test'")
end
it 'should contain value returned from block' do
tag = Helpers.tag(:div) { 'Block value' }
tag.should include('Block value')
end
end
describe 'stylesheet_tag' do
it 'should return link tag' do
Helpers.stylesheet_tag('test').should include('link')
end
it 'should contain path to asset' do
end
end
end
When I run it on local machine all is good, everything pass. But after pushing to GitHub repo Travis fails and write that Object::Sinatra is uninitialized (link) and I haven't idea why.
spec_helper.rb looks:
ENV['RACK_ENV'] = "test"
require 'simplecov'
SimpleCov.start
require File.join(File.dirname(__FILE__), '..', 'boot')
require 'rspec'
require 'capybara/rspec'
require 'rack/test'
require 'factory_girl'
FactoryGirl.find_definitions
Capybara.app = Orodruin.rack
RSpec.configure do |config|
config.include Rack::Test::Methods
config.after(:each) do
MongoMapper.database.collections.each do |collection|
collection.remove unless collection.name.match(/^system\./)
end
end
end
class Helpers
extend(*Sinatra::Base.included_modules.map(&:to_s).grep(/Helpers/).map(&:constantize))
end
because http://travis-ci.org/#!/orodruin/orodruin/jobs/2248831/L73 isn't using bundle exec.
the "bundle exec rake" line above it didn't seem to do anything.
you will need to prefix that line with bundle exec.
I don't see that line in your code, but it could be hard coded in one of your gems or in the Travis service.
The real problem is that the sinatra gem isn't found when Travis is running the specs. This is because travis is using an RVM gemset, and you are probably using the "global" gemset.
The result is ruby -s rspec ... isn't being ran in the gem bundle environment and isn't loading Sinatra.
I've forgot to add require 'spec_helper' on top of my specfile.

Resources