After running zeus start and bundle exec guard, rspec is triggered immediately when I save a file guard is watching. At that point zeus is still reloading the file. Therefore, I have to save the file a second time for the changes to be reflected by rspec. I think it may have something to do with Guardfile:
group :models do
guard :rspec, cmd: 'zeus rspec' do
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^spec/models/.+_spec\.rb$})
watch(%r{^app/models/(.+)\.rb$}) { |m| "spec/models/#{m[1]}_spec.rb" }
end
end
How do I track/solve this problem?
Related
I'm attempting to run some tests on my rails application, and they're working, which is great. However, I'm noticing that when I just run rake it defaults to running my tests. If anybody has run into this before and can shed some light on why this is happening, I'd appreciate it.
I'm using
rails 4.1.0
ruby 2.0.0
factory girl rails
minitest rails
minitest rails capybara
database cleaner
Rakefile
require File.expand_path('../config/application', __FILE__)
Pinteresting::Application.load_tasks
namespace :test do
task :run do
ENV["RACK_ENV"] = "test"
$LOAD_PATH.unshift("lib", "spec")
if ARGV[1]
require_relative ARGV[1]
else
Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
end
end
end
The default rake task is defined in rails/railties/Rakefile, and it runs all unit tests by default.
I have recently tried installing Cucumber to supplement my testing environment. I already had Guard working with Spork and Rspec perfectly. I have set everything up so that it runs now, with Cucumber included, but two things are acting a bit funky.
First issue: when Guard runs the Cucumber tests, it pauses for about 10 seconds after printing "Disabling profiles..." After about 10 seconds, it runs the tests perfectly.
Second Issue: when I make changes to my features (which are stored in subdirectories, such as /features/users/sign_in.feature) or step definitions, guard doesn't run the tests. In order for me to run the tests, I have to manually hit and Guard proceeds to run all tests again. Clearly, that defeats the purpose of guard.
Here is some of my code:
Guard Terminal Output
ben#ben-K53SV:~/rails_projects/katmeer4$ bundle exec guard
16:51:57 - INFO - Guard is using Libnotify to send notifications.
16:51:57 - INFO - Guard is using TerminalTitle to send notifications.
16:51:57 - INFO - Guard::RSpec is running
16:51:57 - INFO - Running all specs
No DRb server is running. Running in local process instead ...
.....[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
........F....
Failures:
1) User abilities admin Index Page
Failure/Error: it {should have_selector('div', text: 'delete')}
Capybara::ExpectationNotMet:
expected to find css "div" with text "delete" but there were no matches
# ./spec/features/role_spec.rb:38:in `block (5 levels) in <top (required)>'
Finished in 1.18 seconds
18 examples, 1 failure
Failed examples:
rspec ./spec/features/role_spec.rb:38 # User abilities admin Index Page
Randomized with seed 16984
16:52:08 - INFO - Starting Spork for RSpec, Cucumber
Using RSpec, Rails
Using Cucumber, Rails
Preloading Rails environment
Preloading Rails environment
Loading Spork.prefork block...
Loading Spork.prefork block...
Spork is ready and listening on 8990!
Spork is ready and listening on 8989!
16:52:18 - INFO - Spork server for RSpec, Cucumber successfully started
16:52:18 - INFO - Running all features
Disabling profiles...
..
P-
(::) pending steps (::)
features/users/sign_in.feature:9:in `Then I see an invalid login message'
1 scenario (1 pending)
4 steps (1 skipped, 1 pending, 2 passed)
0m0.827s
Important Guardfile Code
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
:rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
guard 'cucumber' do
watch(%r{^features/.+\.feature$})
watch(%r{^features/support/.+$}) { 'features' }
watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
end
features/support/env.rb
require 'rubygems'
require 'spork'
Spork.prefork do
end
Spork.each_run do
end
require 'cucumber/rails'
ActionController::Base.allow_rescue = false
begin
DatabaseCleaner.strategy = :transaction
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Cucumber::Rails::Database.javascript_strategy = :truncation
You forgot to split the env.rb code into the prefork and each_run blocks. Try:
require 'rubygems'
require 'spork'
Spork.prefork do
require 'cucumber/rails'
end
Spork.each_run do
ActionController::Base.allow_rescue = false
begin
DatabaseCleaner.strategy = :transaction
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Cucumber::Rails::Database.javascript_strategy = :truncation
end
I'm writing a gem that includes some rake tasks. I have some code in the before_configuration method that I want to run when my gem is loaded by the app at runtime, but NOT when a task is run with rake. How can I determine that?
lib/mygem/tasks.rake:
namespace :mygem do
task :dosomething do
puts "DONE"
end
end
lib/mygem/railtie.rb:
require "rails"
module Mygem
class Railtie < ::Rails::Railtie
config.before_configuration do
#is_rake_task = ?
if !is_rake_task
# Do something
end
end
end
end
Rake is only defined in rake context.
So, you could simply go something like that :
if defined? Rake
# rake specific stuff
else
# non rake stuff
end
Edit :
While this will work perfectly with rails s, there's a problem with zeus on development environment : zeus will require rake.
If this is a problem, if think you can take advantage of Rake.application, which sets an instance variable when a rake task is executed.
I've tested this in a zeus s context :
> Rake.instance_variable_defined? :#application
false
And in a rake <task> context :
> Rake.instance_variable_defined? :#application
true
So, a complete solution would be :
if defined?( Rake ) and Rake.instance_variable_defined?( :#application )
# rake specific stuff
else
# non rake stuff
end
I have a bunch of tests that aren't unit or functional tests, they're of the format test/foo/special_test.rb
I want to create a rake task like rake test:units that will run all the tests in the foo folder. How do I do this?
Edit: I'd actually like rake test:foo to be a little different from rake test:units, in that I do not want it to run when I do simply rake test.
I don't remember where this is from, so unfortunately I can't give proper acknowledgement, but this should work. I say "should" because I've stopped using it, but grabbed it from my git history.
# First, 'reopen' the default :test namespace and create your custom task.
namespace :test do
Rake::TestTask.new(:foo_tests => ["test:prepare", "other_dependent_rake_tasks"] ) do |t|
DatabaseCleaner.strategy = :transaction # If using this.
t.libs << "test"
# Will also get subfolders within test/foo
t.test_files = FileList['test/foo/**/*_test.rb', 'test/foo/*_test.rb']
end
end
You can remove the default "test" task and redefine it so that when you run rake test it will automatically also run rake test:foo_tests.
remove_task "test"
desc 'Adding onto Rails regular tests'
task :test do
# Add all the names of tests you want run here.
errors = %w(test:units test:functionals test:integration test:foo_tests).collect do |task|
begin
puts "Running: #{task}"
Rake::Task[task].invoke
nil
rescue => e
task
end
end.compact
abort "Errors running #{errors * ', '}!" if errors.any?
end
I'm using guard to run all my rails specs and its awesome. I've written a bunch of request specs that use capybara and selenium to test my pages javascripts by opening firefox and they are awesome as well however they tend to be slow and pull focus away from my editor while I'm typing.
Is there a way to configure guard to not run my request specs when it runs all and maybe asign a hot key to just run the request specs?
Answering my own question incase other come across this:
rspec-rails can pass command line arguments to rspec via :cli. Additionally examples can be tagged in spec files and then rspec can be run to include or exclude those tagged examples.
Turns out I'm already tagging the examples I wanted to exclude with :js=>true, wich is how you get Selenium to fire up firefox.
describe "Post" do
it "should be able to edit a post", :js=>true do
# your test here
end
end
I made two groups in my Guardfile one for none-javascript specs with :cli => "-t ~js" and another for spec that test javascript with :cli => "-t js". I also passed in the :all_after_pass => false for the javascript group.
here is my new guard file:
group 'none-javascript specs' do
guard 'rspec', :version => 2, :cli => '-r rspec/instafail -f RSpec::Instafail -t ~js' 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|\.jbuilder)$}) { |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" }
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end
end
group 'javascript specs' do
guard 'rspec', :version => 2, :all_after_pass => false, :cli => '-r rspec/instafail -f RSpec::Instafail -t js' do
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
watch(%r{^spec/requests/.+_spec\.rb$})
end
end
Now when I start up guard or hit return in the guard term both groups are run executing all specs.
Guard::RSpec is running, with RSpec 2!
Running all specs
Run options: exclude {:js=>true}
...
Finished in 75.78 seconds
428 examples, 0 failures, 1 pending
Guard::RSpec is running, with RSpec 2!
Running all specs
Run options: include {:js=>true}
...
Finished in 63.68 seconds
22 examples, 0 failures
After all test pass only the none-javascript examples are run.
There's an exclude option for guard-rspec. It was implemented in #23, but wasn't documented in the README. I've made a pull request to document that.
Example
guard 'rspec', :exclude => "spec/foo/**/*" # exclude files based on glob