rake cucumber hook doesn't work - ruby

every one
I am new with cucumber & rake in ruby. I have completed a example of cucumber test. My test run ok in Ruby Mine by jetbrain (well, hook Before & After work well). When I try to using Cucumber rake task to start cucumber, the cucumber start and run well except the hook script. I found that both Before and After hook (and another hook) doesn't call when rake run the cucumber job
require 'cucumber'
require 'cucumber/rake/task'
require 'rubygems'
require 'rake'
require File.dirname(__FILE__) + "/features/support/selenum_drivers"
task :features => [:chrome_runner]
Cucumber::Rake::Task.new (:chrome_runner) do |t|
SeleniumCommon.configure do |config|
config.selenium_chrome = true
end
t.cucumber_opts = "config=chrome features -f pretty -f progress -r features/all_features -r features/step_definitions"
end
Here is my Rake
Any idea for this.
Thank you

You might try using the cucumber.yml for the cucumber_opts, and placing the other configuration in a support file.
Here a gist I use for javascript, chrome, headless and more.

Related

Rake tasks won't run inside of my sinatra app

I have a sinatra app with a Rakefile.rake and server.rb file. In the server.rb file I have
get '/' do
rake :test
end
the app loads up but crashes when I load localhost:4567/ and says undefined method 'rake' for sinatra application. I try and require the file by using
require '/home/user/project/Rakefile'
and I also try Rakefile.rake but both give me an error that reads "require cannot load such file." I'm using Ubuntu.
I'm not sure why sinatra can't load the rakefile, but when I run rake test in terminal that works.
Any help would be great.
get '/' do
system 'rake test'
end

Accessing the irb in a modular Sinatra application

I am building an application which subclasses Sinatra like so:
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
class App < Sinatra::Base
...
run!
end
How can I access irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell?
Just type as below (at the shell prompt):
irb -r ./my_app.rb
I'm a little confused whether you want to open an IRB session from within your app (?) or use IRB to debug your Sinatra project?
For debugging Rack-based apps (such as Sinatra), I like using the racksh gem, which "is like script/console in Rails" for Rack applications. Its main advantage over IRB is that racksh loads the entire application environment into the shell, making debugging a breeze.
From racksh's Github page: "It's purpose is to allow developer to introspect his application and/or make some initial setup. You can for example run DataMapper.auto_migrate! or make a request to /users/666 and check response details. It's mainly aimed at apps that don't have console-like component (ie. apps built with Sinatra) but all frameworks can benefit from interactive Rack stack and request introspection."
However, racksh requires your app to have a config.ru file, so you would have to re-write your app:
# in config.ru
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
require 'app.rb'
# in app.rb
class App < Sinatra::Base
...
run!
end
Then in your app folder (where config.ru resides):
$ gem install racksh # or add gem 'racksh' to your Gemfile and run bundle
$ racksh
Check this simple search interface for Microsoft's Bing using Sinatra and binger gem.
If you follow the instructions from there you will understand better.
First at all, create a Gemfile and add:
source "https://rubygems.org"
gem 'sinatra'
gem 'binger'
Then run the bundle command that will generated Gemfile.lock.
Then create a config.ru file, and add by example:
require 'rubygems'
require 'bundler'
Bundler.require
require './app.rb'
run MyApp
Your app.rb could look like this:
class MyApp < Sinatra::Base
get '/' do
#title = "Index"
erb:index
end
end
You must have a folder named views. Create index.erb and add:
< % = #title % >
Finally, run rackup.
Source: https://github.com/thinkphp/sinatra-bing
Demo: http://sinatra-bing.herokuapp.com/

Limit rakefile settings to specific enviorment

I have a problem, my rake file looks like this:
require File.expand_path('../config/application', __FILE__)
require 'cucumber'
require "cucumber/rake/task"
Whois::Application.load_tasks
Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format pretty"
end
And the main reason of importing cucumber is to run tests with CI. But recently I've got a problem.
When I deployed to heroku and tried to run heroku run rake assets:precompile, I've got:
rake aborted!
no such file to load -- cucumber
As far as I understand, it can't find the cucumber that's only available in test env.
How can I tell rake, to use cure only in test env?
You'd need to wrap it in something like;
if Rails.env.test?
require 'cucumber'
require "cucumber/rake/task"
end

Running Rake tasks in Rspec Tests

I am building up an integration test suite and there is one bit of logic that I need to have a clean database for. How can I run the db:test:purge task inside of one of my tests?
I am using: ruby 1.9.2, rails 3.0.9, rspec 2.6
You can invoke Rake tasks as following:
require 'rake'
Rake::Task[name].invoke
In this case this would result in the following code:
require 'rake'
Rake::Task['db:test:purge'].invoke
Approved answer didn't work for me, when I needed to execute my own rake task
Here's my solution
Put in the top of the spec file
require 'rake'
Place these lines where you need to execute your custom rake task, e.g. rake update_data from the file example.rake
load File.expand_path("../../../lib/tasks/example.rake", __FILE__)
# make sure you set correct relative path
Rake::Task.define_task(:environment)
Rake::Task["update_data"].invoke
My environment:
rails (4.0.0)
ruby (2.0.0p195)
rspec-core (2.14.7)
rspec-expectations (2.14.3)
rspec-mocks (2.14.4)
rspec (2.14.1)
rspec-rails (2.14.0)
If we require to use multiple rake tasks we can add
require "rake"
Rails.application.load_tasks
Then simply call any task.
Rake::Task['sync:process_companies'].invoke
Though I cant confirm if its slower because it loads all the tasks
for me (rails-6)
Rails.application.load_tasks
Rake::Task['app:sync'].invoke
=> require not necnessary in my case
We need to require the task also
require 'rake'
Rake.application.rake_require 'tasks/new_adapter'
After this, just call the task
Rake::Task['new:adapter'].invoke
Many of the above answers worked for me for running a single spec. However, I had to take an extra step when running multiple specs for a single rake task.
After each spec, I had to run Rake::Task.clear, as (for some reason) the task would not be run again if it was registered as being already_invoked (i.e. if Rake::Task['my_task'].already_invoked returned true.
I added the following line to my rake task spec:
after { Rake::Task.clear }
and everything worked as expected when running multiple tests for the same rake task.

deploy rake task as if it were a common script

I like the rake task structure and rake utilities.. I want to create a script that can do many things, and rake seems the logical choice.
I want to know how can I make it look like a regular script:
./myscript.rb cake:bake
as opposed to
rake -f myscript.rb cake:bake
any idea? Of course, rake must still be installed etc.. simply to make it easier to use...
myscript.rb:
#!/usr/bin/ruby
require 'rubygems'
require 'rake'
namespace :cake do
task :bake do
puts "Baking cake..."
end
end
Rake::Task[ARGV.first].execute
Then on the command line:
chmod +x myscript.rb
./myscript.rb cake:bake
I found this for cygwin / windows
http://errtheblog.com/posts/60-sake-bomb
removes the dependency on rails and let's you have rake tasks installed and available globally

Resources