My micro Ruby project tests works on my machine, on private VM, but fails on Travis CI bulid
Project uses both rspec (+rspec-given) and minitest
Full trace here: https://travis-ci.org/equivalent/code_katas/jobs/61321482
$ bundle exec rake
MiniTest::Unit::TestCase is now Minitest::Test. From /home/travis/.rvm/rubies/ruby
2.1.4/lib/ruby/2.1.0/test/unit/testcase.rb:8:in `<module:Unit>'
/home/travis/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/test/unit.rb:676:in `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner' (NameError)
from /home/travis/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/test/unit.rb:261:in `<module:Unit>'
from /home/travis/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/test/unit.rb:15:in `<module:Test>'
from /home/travis/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/test/unit.rb:7:in `<top (required)>'
from /home/travis/build/equivalent/code_katas/vendor/bundle/ruby/2.1.0/gems/sorcerer-1.0.2/test/sorcerer/resource_test.rb:3:in `require'
#...
The error is self-explanatory MiniTest::Unit::TestCase is now Minitest::Test. This is caused by rspec-given dependency sorcerer using test-unit (source) (My code is not referring to test-unit anywhere in the code)
So the thing I'm curious about is if anyone know how to get around this.
Like I said it works everywhere else except Travis CI
Problem was that Travis CI is cloning repository into /home/travis/build/username/projectname and then bundle install with
--deployment flag which install gems to vendor folder
/home/travis/build/username/projectname/verdor
my Rakefile was using test pattern **/*_test.rb therefore run tests in all folders, therefore it was loading verdor directory and all the tests from dependencies (external gems) some of which were TestUnit not compatible with MiniTest
solution was to load tests in Rakefile not with pattern but with:
Rake::TestTask.new(:test) do |t|
t.test_files = Dir['**/*_test.rb'].reject do |path|
path.include?('vendor') # tell travis CI to ignore vendor tests
end
end
similar apply for RSpec
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = Dir.glob('**/*_spec.rb').reject do |path|
path.include?('vendor') # tell travis CI to ignore vendor tests
end
# t.rspec_opts = '--format documentation'
end
Related
Working on a Ruby gem and trying to use FactoryBot inside with RSpec.
I have this in support/factory_bot.rb:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
and in spec_helper.rb:
require 'support/factory_bot'
When I try to run the spec rake task, I get this error:
support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError)
What am I missing? This used to work fine when I was using the old factory_girl gem, but it has broken with the rename to factory_bot. Thanks!!
Doh. Silly mistake here, running bundle exec rake spec instead of rake spec solved it.
Also had to add require 'factory_bot' to the top of support/factory_bot.rb
Overview just in case you are doing this from scratch
installation rspec details here (basically add gem to Gemfile then run bundle install)
initialize RSPEC in your rails project rails g rspec:install
create new file your spec/support/factory_bot.rb add the following base code:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
add reference on spec/rails_helper.rb
require 'support/factory_bot'
as well as remove any fixture unused reference like this one
config.use_transactional_fixtures = true
That should be it!, finally run any spec file you want inside rspec default folders
e.g.:
spec/features/my_action_spec.rb
spec/models/my_model_spec.rb
spec/task/my_task_spec.rb
or run them all and check depending on your setup
rspec
rails rspec
bundle exec rspec
hope this helps someone with the whole RSPEC + FactoryBot Gem installation process
I had this problem too, remove the
require 'support/factory_bot'
There's a line on rails_helper, just uncomment it:
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
I had encountered and issue similar to this. I worked around it by removing the default testing suite ( MiniTest ). When you create a rails application and intend on using rspec and factory_bot, use the code below in the command line:
rails new myapp -T
Hope this helps xP
In my case I had to put those lines below 'require "rspec/rails" in file:
spec/rails_helper.rb:
like:
require "rspec/rails"
require_relative "support/factory_bot"
require_relative "support/chrome"
I would like to do the testfirst.org exercises although can't seem to get to the starting line due to a version issue/lack of knowledge on my part as well..
At the begining you have to run the test $rake
i got following error..
(in /home/arno/learn_ruby)
rake aborted!
Gem::MissingSpecError: Could not find 'rspec' (~> 2) among 94 total
gem(s)
Checked in
'GEM_PATH=/home/arno/.gem/ruby/2.4.0:/home/arno/.rbenv/
versions/2.4.0/lib/ruby/gems/2.4.0', execute `gem env` for more
information
/home/arno/learn_ruby/Rakefile:2:in `<top (required)>'
(See full trace by running task with --trace)
I then tried to uninstall rspec. I then tried to install a version less than 3. That also did not work. Now I have version 2.9 and 3.2 installed but the same error persisted..
I then did bundle install and bundle update...something changed although still not working..
Here is the latest error when i run the test $rake..
(in /home/arno/learn_ruby)
rake aborted!
NoMethodError: undefined method `last_comment' for #
<Rake::Application:0x00564ff13bf4b0>
/home/arno/learn_ruby/Rakefile:8:in `new'
/home/arno/learn_ruby/Rakefile:8:in `<top (required)>'
(See full trace by running task with --trace)
Here is the rakefile..
# This Rakefile has all the right settings to run the tests inside
each lab
gem 'rspec', '~>2'
require 'rspec/core/rake_task'
task :default => :spec
desc "run tests for this lab"
RSpec::Core::RakeTask.new do |task|
lab = Rake.application.original_dir
task.pattern = "#{lab}/*_spec.rb"
task.rspec_opts = [ "-I#{lab}", "-I#{lab}/solution", '-f
documentation', '-r ./rspec_config']
task.verbose = false
end
After 2 days of searching i found a solution from The Odin Project.
They have a Test First - Rspec 3 edition..
If your having the same issue go to their repo https://github.com/TheOdinProject/learn_ruby.
Read through the readme file, fork that repo, clone your version of the repo, cd into it and bundle install..
All works fine now..
I have some classes with unit tests and I have a rake task to run these unit tests. However, I'm running into a problem where when the tests are run via rake an old version of minitest is being used. How can I get rake to use the newer version?
If I use the Minitest::Test subclass it runs fine when the tests are directly run through the ruby
command-line. However, if I use the following rake task:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = 'tests/**/*_test.rb'
end
When I check the minitest version using puts MiniTest::Unit::VERSION it prints 5.5.0 when run with ruby, but prints 4.3.2 when run with rake. (When using gem list minitest -d version 4.3.2 is listed as the default.)
The reason I want to use the newer version of minitest is that when I directly run the unit tests using Ruby 2.0 I get the following warning:
MiniTest::Unit::TestCase is now Minitest::Test.
However, if I change MiniTest::Unit::TestCase to Minitest::Test I get the following error when I run the tests using rake.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/ext/module.rb:36:in `const_missing': uninitialized constant MiniTest::Test (NameError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/minitest/unit.rb:28:in `const_missing'
I want to avoid requiring any changes to the system configuration, because I want these tests to run on the default OS X ruby installation.
Using gem 'minitest', '=5.5.0' does not change the version of minitest that gets used.
Possibility #1: Run Rake with bundle exec to force it to use the version of Minitest managed by Bundler:
bundle exec rake test
Possibility #2: Create a binstub for Rake for your project that will load your bundle automatically:
bundle binstub rake
After that, you should be able to run Rake without bundle exec to get the same result.
Caveat: This has worked for the Ruby environment managers I've used recently, but you might need to Google around for a solution if it doesn't work for you.
Ruby is having a hard time loading minitest from the gem instead of the standard library, so we need to give it a little help. Add the following to your helper or rake task:
gem "minitest"
That will tell ruby to use the gem version. Just be sure to add that before minitest is required.
I am trying to run some cucumber tests using rake. When I call rake kickoff (#kickoff is the tag within my feature file) I get the following error:
rake aborted!
NoMethodError: undefined method `initializer' for Cucumber:Module
/webdata/jenkins/jobs/kickoff_build/workspace/Rakefile:2:in `<top (required)>'
(See full trace by running task with --trace)
I have already run bundler and all necessary gems are installed.
Here is the contents of my kickoff_build feature file:
#kickoff
Feature: Automated Regression Build
Scenario: As an automation developer, I want to kick off the build
Given the CI server is configured correctly
When the tag in line 1 of this file is called via a rake task
Then this test should run and pass
Here is the content of my Rakefile:
require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
require 'cuke_sniffer'
Cucumber::Rake::Task.new(:kickoff, :build) do |t|
tags = '--tags ~#wip --tags ~#manual --tags ~#known_defect'
t.cucumber_opts = "--format html --out results/result.html --format pretty #{tags} --format junit --out features/reports"
end
This is a bug present in the betas for cucumber 2.0.0.
Upgrade to cucumber ~> 2.0 and you should be fine.
What is the process for doing TDD in Ruby with RSpec without Rails?
Do I need a Gemfile? Does it only need rspec in it?
Ruby 1.9.3
The process is as follows:
Install the rspec gem from the console:
gem install rspec
Then create a folder (we'll name it root) with the following content:
root/my_model.rb
root/spec/my_model_spec.rb
#my_model.rb
class MyModel
def the_truth
true
end
end
#spec/my_model_spec.rb
require_relative '../my_model'
describe MyModel do
it "should be true" do
MyModel.new.the_truth.should be_true
end
end
Then in the console run
rspec spec/my_model_spec.rb
voila!
From within your projects directory...
gem install rspec
rspec --init
then write specs in the spec dir created and run them via
rspec 'path to spec' # or just rspec to run them all
The workflows around gem install rspec are flawed. Always use Bundler and Gemfile to ensure consistency and avoid situations where a project works correctly on one computer but fails on another.
Create your Gemfile:
source 'https://rubygems.org/'
gem 'rspec'
Then execute:
gem install bundler
bundle install
bundle exec rspec --init
The above will create .rspec and spec/spec_helpers.rb for you.
Now create your example spec in spec/example_spec.rb:
describe 'ExampleSpec' do
it 'is true' do
expect(true).to be true
end
end
And run the specs:
% bundle exec rspec
.
Finished in 0.00325 seconds (files took 0.09777 seconds to load)
1 example, 0 failures