How to move rspec tests into integration folder - ruby

At the minute i have rspec tests in a folder called views within the spec folder and the tests run fine. What im looking to do is move the rspec tests to the integration folder within the test folder than gets generated at the start when you create an new app.
When i move the rspec tests and try and run them i get an error saying it cant find the spec_helper.rb so ive transferred that info into the test_helper.rb. When i try and run the test it now says it cant find the test_helper.rb. The test_helper.rb is located in the test folder.
spec_helper.rb code
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require "rspec"
require 'rspec/rails'
require "rubygems"
require "capybara/rspec"
require "capybara/dsl"
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara::DSL
config.formatter = :documentation # :progress, :html, :textmate
end
Capybara.configure do |config|
config.run_server=true
config.default_driver=:selenium
end
end
So is there a configuration somewhere that i have to change. Any thoughts or suggestions?
Thanks
Ray

Related

All SauceLabs tests run with Capybara result in "Unnamed Ruby job" and no metadata

I'm setting up a standalone RSpec/Capybara test suite integrated with SauceLabs, but the instructions in the documentation don't seem to be working for me.
Here are the relevant parts of my spec_helper.rb:
require 'capybara'
require 'capybara/rspec'
require 'sauce/capybara'
Sauce.config do |config|
config[:browsers] = [
[ "OSX 10.10", "Safari", "8" ]
]
end
Capybara.default_driver = :sauce
And here's the feature (not_found_spec.rb):
feature 'Enroll: 404', :sauce => true do
before :each do
#nonexistent_curriculum = FactoryGirl.build :curriculum
#enroll = Enroll.new
end
context 'When I visit a page that does not exist' do
scenario 'I see a Not Found message' do
#enroll.go #nonexistent_curriculum
expect(#enroll.not_found).to be_visible
end
end
end
When I then run rspec, the specs run and pass, but no metadata of any kind is recorded. All I see on SauceLabs is "Unnamed Ruby job".
What am I missing?
When you run
bundle exec rake sauce:install:spec
it creates a sauce_helper.rb which is then typically required from the end of your rails_helper.rb or spec_helper.rb depending on what *_helper.rb files you have. It looks like you copied the Sauce config part from sauce_helper into your spec_helper but you haven't shown that you have
require "sauce"
in there which is in the generated sauce_helper. Without requiring "sauce" it may be that sauce/rspec/rspec.rb is not getting required which is where all the hooks into rspec for tests with sauce: true are set up.

How to embed a screenshot with minitest-reporters

I am using minitest with Jenkins to produce test reports. At the moment I am using Minitest::Reporters::JUnitReporter - https://github.com/kern/minitest-reporters. This seems to give nice concise results.
The only thing missing is an embedded screenshot, specifically from failed tests.
How can I produce a Jenkins friendly test report that includes a screenshot? I am open to use one of the other Minitest::Reporters if it helps.
Thanks
You can use the capybara-screenshot gem for this: https://github.com/mattheworiordan/capybara-screenshot
Gemfile:
group :test do
gem 'capybara', '2.6.0'
gem 'selenium-webdriver', '2.49.0'
gem 'poltergeist', '1.8.1'
gem 'capybara-screenshot', '1.0.11'
end
test_helper.rb:
# default rails test configuration
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
# configure webtests with capybara and poltergeist (headless)
require 'capybara/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/minitest'
if ENV['HEADLESS'] == 'true'
# headless driver configuration
Capybara.default_driver = :poltergeist
else
Capybara.default_driver = :selenium
end
Capybara::Screenshot.prune_strategy = :keep_last_run # keep screenshots only from the last test run
Capybara.save_and_open_page_path = File.join(Rails.root, "test/screenshots") # where to save screenshots
# default test class for unit tests
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
# default test class for webtests
class ActionDispatch::IntegrationTest
include Capybara::DSL
include Capybara::Screenshot::MiniTestPlugin
end

Running rspec in Sinatra app just starts the server

I'm using rspec to test my Sinatra app. The app is super simple, and so are the tests, but when I run rspec from the CLI, it just starts the app server. I've never had this happen.
Here's what my spec_helper.rb looks like:
#spec/spec_helper.rb
require File.expand_path '../../app.rb', __FILE__
ENV['RACK_ENV'] = "test"
require 'rspec'
require 'rack/test'
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
module RSpecMixin
def app
App.new
end
end
RSpec.configure do |config|
config.color_enabled = true
config.tty = true
config.formatter = :documentation
config.include Rack::Test::Methods
config.include RSpecMixin
end
And my spec is just
require 'spec_helper'
describe "My Sinatra Application" do
it "should allow accessing the home page" do
expect(1).to eq(1)
end
end
I can't get rspec to run.
What I've tried:
I've tried the recommendations in this Sinatra testing guide. Also the rspec Sinatra test recipe. In this blog post, the set :run, false looked promising, but no dice. Then I thought, I'll just define a rake task. Putting the rspec gem in the test group and setting the RACK_ENV to test. All these things just start the app server:
$ rake spec
[2014-03-08 22:06:38] INFO WEBrick 1.3.1
[2014-03-08 22:06:38] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2]
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from WEBrick
[2014-03-08 22:06:38] INFO WEBrick::HTTPServer#start: pid=21538 port=4567
Halp?
Try removing the new() from your def app:
module RSpecMixin
def app
App
end
end
An other way to create your app for testing is something like this:
APP = Rack::Builder.parse_file('config.ru').first
module RSpecMixin
def app
APP
end
end
Obvious this works only if you use config.ru.
I found the answer. It's embarrassing, but I was switching back and forth between the modular and classic style deciding which I liked best, and I had left an errant App.run! call at the bottom of my app.rb file.

Minitest uninitialized constant error

I am trying to run Minitest with Spec syntax with rake test and get this error:
/path/to/gem/spec/script_spec.rb:3:in `<top (required)>': uninitialized constant MyGem (NameError)
My Rakefile:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.test_files = FileList['spec/*_spec.rb']
end
My file structure:
gem/
--lib/
----script.rb
--spec/
----script_spec.rb
--Rakefile
My script.rb:
module MyGem
class OptionParser
def self.option?(arg)
arg =~ /^-{1,2}\w+$/
end
end
end
Using Minitest::Spec syntax in script_spec.rb:
require "minitest/autorun"
describe MyGem::OptionParser do
describe "option?" do
it "must be true for option name" do
OptionParser.option?('--nocolor').assert true
end
end
end
How do I fix it? Maybe lib folder isn't loaded? Do I miss something related to Spec syntax?
MyGem::OptionParser is not loaded in your tests. You either need to require it in your spec file or create a spec_helper where you require all files that you need in all your tests so you only need to require 'spec_helper' in your specs.
Also, since you're using the spec syntax, you will have to `require 'minitest/spec'. Your spec_helper would look something like:
# spec/spec_helper.rb
require 'minitest/spec'
require 'minitest/autorun'
require 'script'
And do this to your Rakefile so you can do require 'script' like above in your specs instead of doing require_relative '../lib/script'.
require 'rake/testtask'
Rake::TestTask.new do |t|
t.test_files = FileList['spec/*_spec.rb']
end
Lastly, for your spec to work, add require 'spec_helper' at the top of your script_spec file. You'll have to do this for all your spec files and make sure to add require for all the files you need to load in your specs to your spec_helper file.
Since you're also doing spec-style testing, you might want to change your test to this:
MyGem::OptionParser.option?('--nocolor').must_equal true
You could also have code like this in your 'spec_helper' file to automatically load all files in your lib folder:
Dir["../lib/**/*.rb"].each do |rb_file|
require rb_file
end
Hope this helps!

selenium suite in ruby/rspec - how to run multiple cases and stay logged in?

I have a selenium suite in rspec.
How can I run multiple cases and keep the browser open and logged in between them?
I have a test suite in the selenium IDE that is comprised of about 20 unit cases.
I've exported both the individual test cases and the test suite file itself to ruby/rspec
I can run individual tests and they pass, e.g.
rspec spec/2day/units/set_QA_district_name_spec.rb
However when I tried to run the converted suite with either
rake spec
or
rspec spec/2day/complete_district_suite_spec.rb
it runs each spec one-by-one by bringing up (and then closing) the application for each test.
How can I run the suite and have the browser 'stay' up and logged in from test to test and not be opening and then closing down the browser window for each unit test that runs.
I have to run these tests in the standalone directory that I've created as they have to be kept separate from the rest of the applications test areas, in other words I have to avoid any use of rails itself.
I have set up the following Gemfile / spec_helper / Rake files in the root directory for these files as follows:
Gemfile:
gem 'rspec'
gem 'selenium-webdriver'
spec_runner.rb:
require "selenium-webdriver"
require 'rspec'
Rakefile
require 'rspec'
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.ruby_opts = %w[-w]
t.rspec_opts = %w[--color]
end
The contents of complete_district_suite.rb are:
ENV['RSPEC_COLOR'] = 'true'
require File.join(File.dirname(__FILE__), "units/set_QA_district_name_spec.rb")
require File.join(File.dirname(__FILE__), "units/set_file_uploads_source_location_spec.rb")
require File.join(File.dirname(__FILE__), "units/district_spec.rb")
require File.join(File.dirname(__FILE__), "units/select_district_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_service_types_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_services_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_grades_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_schools_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_classrooms_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_students_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_ieps_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_travel_spec.rb")
require File.join(File.dirname(__FILE__), "units/manual_add_student_spec.rb")
require File.join(File.dirname(__FILE__), "units/generate_basic_schedule_spec.rb")
require File.join(File.dirname(__FILE__), "units/wait_for_dss_to_finish_spec.rb")
require File.join(File.dirname(__FILE__), "units/view_schedules_spec.rb")
require File.join(File.dirname(__FILE__), "units/visit_first_schedule_spec.rb")
require File.join(File.dirname(__FILE__), "units/monday_1pm_new_appt_spec.rb")
require File.join(File.dirname(__FILE__), "units/tuesday_2pm_new_5_students_spec.rb")
require File.join(File.dirname(__FILE__), "units/wednesday_9am_5pm_new_1_student_spec.rb")
require File.join(File.dirname(__FILE__), "units/thursday_9am_10am_new_1_manual_add_student_spec.rb")
require File.join(File.dirname(__FILE__), "units/friday_10am_11am_new_5_students_spec.rb")
I think you are looking for before(:all)
describe "Search page" do
before(:all) do
#browser = Watir::Browser.new
end
it "should find all contacts" do
...
end
after(:all) do
#browser.kill! rescue nil
end
end
More info here
Suggestion
Also you could include all your spec files in one line by so :-
Dir[File.join(File.dirname(__FILE__), 'units') + "/*_spec.rb"].each { |file|
require file
}

Resources