I would like to be able do something like the following:
#javascript #disallow_cookies
Scenario: Test Something that depends on cookies
How would I write the #disallow_cookies cucumber hook to achieve this?
I am using the following:
gem 'thin', '1.2.11'
gem 'cucumber-rails' , '1.0.4'
gem 'database_cleaner', '0.6.7'
gem 'capybara', '1.1.2'
gem 'capybara-firebug','0.0.10'
gem 'selenium-webdriver', '2.14.0'
Thanks!
This is an old question but here goes. Create a before hook for the tag #disallow_cookies.
Before('#disallow_cookies') do
profile = Selenium::WebDriver::Firefox::Profile.new
profile['network.cookie.cookieBehavior'] = 2 # disables all kind of cookies
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
Assuming that you are creating the driver in the general before hook you should be fine. I haven't run this code but it is where I would start the process.
Related
I'm using the Mocha gem in a Rails project to make stubs and bypass some live API calls.
I'm conforming to the documentation but keep getting an error.
In Gemfile (I use Bundler) I have:
gem 'minitest'
gem 'minitest-rails'
gem 'mocha', :require => false
In test_helper.rb:
require 'minitest'
require 'mocha/mini_test' # This is the last require statement
In product_controller_test.rb:
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
setup do
Product.any_instance.stubs({
manufacturer: "Acme"
})
end
end
The output is always NoMethodError: undefined method 'any_instance' for #<Class:blah>.
I have another Rails project where I can use any_instance successfully, so I've tried resolving by replicating the gems, require statements, etc. but with no success. I've also tried different permutations like require 'minitest/autorun', require "minitest/rails', and require 'mocha/setup'/require 'mocha', etc.
I'm on Rails 4.1.1 and using MiniTest 5.3.3 and Mocha 1.0.0.
I have a resque worker which works great but is just too slow. The main reason for this is I'm using activerecord and having to load the entire environment which takes at least 10-20 seconds just to load up (I don't keep a running worker at all times as I'm using Heroku and pay for the time the worker runs). I'm using a resque worker to grab & parse data from an external website and then dumping the data into my database.
My question is whether I should rewrite the method to not use Rails and instead use DataMapper? Or something else which would load faster than activerecord.
Or If I should extract the code (using ActiveRecord) which figures out what to do with the external data and move it out of the worker and back into the app?
Hope that makes sense.
I have the same problem.
you could setup your environment on the rake resque:setup rake task
I tried this. assuming my rake resque task is on lib/tasks/resque.rake
require "resque/tasks"
task "resque:setup" do
root_path = "#{File.dirname(__FILE__)}/../.."
db_config = YAML::load(File.open(File.join(root_path,'config','database.yml')))["development"]
ActiveRecord::Base.establish_connection(db_config)
require "#{root_path}/app/workers/photo_downloader.rb" #workers
#Dir.glob("#{root_path}/app/models/*").each { |r| puts r; require r } #require all model
require "#{root_path}/app/models/photo.rb" # require model individually
end
I haven't completely success beacuse I use the Paperclip gem which require rails environment
Rails’ bootstrap is really slow; it is intended to be kept running, until certain time for restart (to eliminate some memory leaks there most likely is, any software is not bug-free), and is not intended to be used as a site that is launched for one request and then shut down.
That kind of usage more resembles a script. If you need to launch it with browser, you can easily use something like Erubis to write the page and use ActiveRecord in the script (I think it was useable outside of rails) or similar abstraction layer. Myself, for small tasks, I just use Mysql2.
Use bundler to get active_record and other gem to you without rails application .
require 'rubygems'
require 'logger'
require 'active_record'
require 'bundler'
require "active_support"
require "spreadsheet"
require 'net/ping'
require 'net/http'
Bundler.setup
Bundler.require(:default) if defined?(Bundler)
$config_logger = Logger.new("./log/dev.log")
class Dbconnect
def initialize
#settings = YAML.load_file('./config/database.yml')["development"]
#adapter = #settings["adapter"] if #settings["adapter"]
#database = #settings["database"] if #settings["database"]
#pool = #settings["pool"] if #settings["pool"]
#timeout = #settings["timeout"] if #settings["timeout"]
end
def connect_to_db
ActiveRecord::Base.establish_connection(
:adapter => #adapter,
:database => #database,
:reconnect => #reconnect,
:pool => #pool,
:timeout => #timeout)
$config_logger.info "\n db Connected: to => #{#database} "
end
end
end
}
Example Gemfile :
source "http://rubygems.org"
gem 'mail'
gem "escape_utils"
gem 'json',:require => "json"
gem 'json_pure'
gem 'resque'
gem 'resque-scheduler'
gem 'redis-namespace'
gem 'resque-status'
gem 'rake'
gem 'em-udns'
gem 'sqlite3'
gem 'spreadsheet'
gem 'activerecord', '3.2.1', :require => "active_record"
gem 'net-scp', :require => 'net/scp'
gem 'net-sftp', :require => 'net/sftp'
gem 'net-ssh', :require => 'net/ssh'
gem 'dir'
gem 'amatch'
gem 'haml'
gem 'net-ping'
gem install bundler
rest of the thing : bundle install .
I'm likely doing something very simply wrong, but I'm not quite sure what it is. I am porting a rails 2 application to rails 3. This application uses webmock for a bunch of it's tests.
If I include
gem 'webmock'
In my Gemfile, the tests pass, but when I start the server and run the app locally, hitting a controller that should make a web call throws an error:
WebMock::NetConnectNotAllowedError
If I do NOT include the line in my Gemfile, then when I run the app locally, it works fine, but the tests error out with:
`require': no such file to load -- webmock (LoadError)
When this line is hit in my test_helper.rb
require 'webmock'
I'm guessing I've got something configured wrong, but I haven't hit the right google incantation to shed any light on it yet. Where I did I go astray?
Thank you.
Try telling your Gemfile to only load webmock when you're in a test environment:
group :test do
gem "webmock"
end
On my Ruby 1.9 Rails 3 instance I have something like the following:
group :test do
gem "mocha"
gem "webmock"
end
group :development do
gem 'ruby-debug19', :require => 'ruby-debug'
end
I'm having a hard time getting a simple file upload test working. I'm using Rails 3.0.0 on ruby 1.9.2 with Cucumber and Capybara.
View:
<%= form_tag "/upload/create", :multipart => true do %>
<label for="file">File to Upload:</label>
<%= file_field_tag "file" %>
<%= submit_tag "Upload" %>
<% end %>
Cucumber Step:
When /^I upload the basic file$/ do
visit path_to("upload")
path = File.join(::Rails.root, "somefile")
attach_file("file", path)
click_button("Upload")
end
In my controller, i have commented out everything except for:
def create
file = params[:file]
end
Gemfile snippet:
group :development, :test do
# testing with specs
gem "ZenTest", ">= 4.3.3"
gem "autotest"
gem "rspec-rails", ">= 2.0.0.beta.19", :git => "git://github.com/rspec/rspec-rails.git"
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
# cucumber stuff
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy' # So you can do Then show me the page
gem 'escape_utils' # needed to fix Cucumber - http://crimpycode.brennonbortz.com/?p=42
end
When I try to run the test, I receive:
(::) failed steps (::)
bad content body (EOFError)
<internal:prelude>:10:in `synchronize'
I appreciate any help or insight. Thanks.
This turned out to be an issue with rack-test and it probably won't be a problem for most until more people adopt Rails3 and Ruby 1.9.x.
Upgrading rack-test to the current master branch fixed the problem.
I'm not sure when rack-test will include these changes in the gem.
See also:
groups.google.com/group/cukes/browse_thread/thread/5028306893c2c54a
I don't have an answer but working on the same problem in the same environ - cukes, capybara, rails 3, 1.9.2.... if I figure this out will let you know. Have you thought of posting on the cucumber google group or the Rails google group? If you don't once I get my act together and cant figure out will post to one of these.
Also, it seems that webrat has the method for attach_file() and thus when I generated cucumber without capybara it had a corollary method in web_steps.rb, but after I added capybara and regenerated cucumber it was gone....
I am trying to install vote-fu into my rails 3 project.
The documentation ( for rails 2 ) says to install it into my environments.rb file as so..
config.gem "peteonrails-vote_fu", :lib => 'vote_fu', :source => 'http://gems.github.com'
How could I convert that to rails 3 for the Gemfile?
In Gemfile:
gem 'vote_fu'
http://gems.github.com is no more. The standard gem host is http://gemcutter.org which Bundler is setup to use by default, so you don't need to specify source. Vote_fu is hosted on gemcutter (see: http://rubygems.org/gems/vote_fu).
For reference, you would convert the options in config.gem like this:
:source => 'example.com' changes to (on its own line in Gemfile):
source 'example.com'
:lib => 'mylib' changes to:
gem 'libkey_mylib', :require => 'mylib'