EzDevInfo.com

rspec-rails

RSpec extension library for Ruby on Rails

RSpec: describe, context, feature, scenario?

describe, context, feature, scenario: What is the difference(s) among the four and when do I use each one?


Source: (StackOverflow)

expected true to respond to true?

I upgraded my rspec-rails to 3.0.1 and now I'm seeing this error on all of my tests

 Failure/Error: Sidekiq::Status::complete?(json.jid).should be_true
  expected true to respond to `true?`

I can't find the solution nor what I'm missing.


Source: (StackOverflow)

Advertisements

Rspec/Capybara loading in progress, circular require considered harmful

Unsure if this is a Capybara issue, but I just did a fresh install of Rails (4.1.1) and Rspec Rails (3.0.1) and I would like to use Capybara (2.3.0), but having never used it I'm running into issues. I created a "spec" in spec/features and the spec itself runs and passes as expected, but I get the following "warnings" when rspec is run. Any ideas? Do I just need to disable warnings at the ruby level or something?

/Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/mail-2.5.4/lib/mail/network/delivery_methods/sendmail.rb:53: warning: shadowing outer local variable - to
/Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247: warning: loading in progress, circular require considered harmful - /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara.rb
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/ruby_executable_hooks:15:in  `<main>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/ruby_executable_hooks:15:in  `eval'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/rspec:23:in  `<main>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/rspec:23:in  `load'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/exe/rspec:4:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:38:in  `invoke'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:70:in  `run'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:85:in  `run'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:97:in  `setup'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in  `load_spec_files'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in  `each'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in  `block in load_spec_files'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in  `load'
    from /Users/typeoneerror/Dev/Doki/engines/doki_core/spec/features/accounts/sign_up_spec.rb:1:in  `<top (required)>'
    from /Users/typeoneerror/Dev/Doki/engines/doki_core/spec/features/accounts/sign_up_spec.rb:1:in  `require'
    from /Users/typeoneerror/Dev/Doki/engines/doki_core/spec/rails_helper.rb:7:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in  `load_dependency'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `block in require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-rails-3.0.1/lib/rspec/rails.rb:9:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in  `load_dependency'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `block in require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-rails-3.0.1/lib/rspec/rails/vendor/capybara.rb:2:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in  `load_dependency'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `block in require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara/rspec.rb:1:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in  `load_dependency'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `block in require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara.rb:5:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara.rb:320:in  `<module:Capybara>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in  `load_dependency'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `block in require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara/dsl.rb:1:in  `<top (required)>'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in  `load_dependency'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `block in require'
    from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in  `require'
/Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara/node/matchers.rb:121: warning: assigned but unused variable - e
/Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara/selenium/driver.rb:119: warning: assigned but unused variable - a

Source: (StackOverflow)

Object.any_instance should_receive vs expect() to receive

The following piece of code works as expected:

Object.any_instance.should_receive(:subscribe)

But when using the new rspec expectation it does not work:

expect(Object.any_instance).to receive(:subscribe)

The error is:

expected: 1 time with any arguments
received: 0 times with any arguments

How can I make this work with expect() to receive?


Source: (StackOverflow)

test a file upload using rspec - rails

I want to test a file upload in rails, but am not sure how to do this.

Here is the controller code:

def uploadLicense
    #Create the license object
    @license = License.create(params[:license]) 


    #Get Session ID
    sessid = session[:session_id]

    puts "\n\nSession_id:\n#{sessid}\n"

    #Generate a random string
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(5) { |i| newpass << chars[rand(chars.size-1)] }

    #Get the original file name
    upload=params[:upload]
    name =  upload['datafile'].original_filename 

    @license.format = File.extname(name)

    #calculate license ID and location
    @license.location = './public/licenses/' + sessid + newpass + name 

    #Save the license file
    #Fileupload.save(params[:upload], @license.location) 
    File.open(@license.location, "wb") { |f| f.write(upload['datafile'].read) }

     #Set license ID
    @license.license_id = sessid + newpass

    #Save the license
    @license.save

    redirect_to :action => 'show', :id => @license.id 
end

I have tried this spec, but it doesnt work:

it "can upload a license and download a license" do
    file = File.new(Rails.root + 'app/controllers/lic.xml')
    license = HashWithIndifferentAccess.new
    license[:datafile] = file
    info = {:id => 4}
    post :uploadLicense, {:license => info, :upload => license}
end

How can I simulate the file upload, using rspec?


Source: (StackOverflow)

Rails 3.1, RSpec: testing model validations

I have started my journey with TDD in Rails and have run into a small issue regarding tests for model validations that I can't seem to find a solution to. Let's say I have a User model,

class User < ActiveRecord::Base
  validates :username, :presence => true
end

and a simple test

it "should require a username" do
  User.new(:username => "").should_not be_valid
end

This correctly tests the presence validation, but what if I want to be more specific? For example, testing full_messages on the errors object..

it "should require a username" do
  user = User.create(:username => "")
  user.errors[:username].should ~= /can't be blank/
end

My concern about the initial attempt (using should_not be_valid) is that RSpec won't produce a descriptive error message. It simply says "expected valid? to return false, got true." However, the second test example has a minor drawback: it uses the create method instead of the new method in order to get at the errors object.

I would like my tests to be more specific about what they're testing, but at the same time not have to touch a database.

Anyone have any input?


Source: (StackOverflow)

Unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)

While testing scenario by cucumber i'm getting the following error when running rspec tests

unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) (Selenium::WebDriver::Error::WebDriverError)

Using ruby (1.9.2) selenium-webdriver (2.27.2) and firefox (19.0)

Using rspec-rails (2.12.1), capybara (2.0.2) and several other gems, Also i have added launchy gem but they don't seem to be a problem. And i am using Windows 7.


Source: (StackOverflow)

Testing Rails 3.1 mountable engine with Rspec

I started making a Rails 3.1 engine, and I'm having a hard time testing it using rspec.

First of all, if I run rails g integration_test whatever it creates a regular integration test in tests/integration instead of spec/requests (the rspec-rails gem is installed and required as a development dependency in the gemspec file)

Also, when I run a spec test I get an error saying the table corresponding to the model I'm testing has not been created. I tried rake engine_name:install:migrations and running rake db:migrate from inside the dummy app, and I get a "table already exists" error.

Everything just seems disconnected, I feel I'm missing something here to make the rspec gem work seamlessly as it usually does with full rails applications.

I followed all the changes from here http://rubyx.com/2011/03/01/start-your-engines and I can test the engine manually by launching the dummy app via the console as shown here http://railscasts.com/episodes/277-mountable-engines.

Is there a way to make rspec the default for testing a rails 3.1 engine?


Source: (StackOverflow)

Rails 3.1 plugin gem, dummy test app, rspec

So Rails 3.1 comes with a little-known handy "rails g plugin new" generator, which gives you a skeleton suitable for a rails gem plugin. [http://guides.rubyonrails.org/plugins.html#or-generate-a-gemified-plugin]

One of the useful things this does, is set things up conveniently for testing with Test::Unit. It gives you a basic dummy Rails app that your tests can run in the context of, to test 'engine' behavior that only functions in the copy of a Rails app. (it puts it in ./test/dummy). But your tests are still in my_gem/test , the tests dont' live in the dummy app. And my_gem/test/test_helper.rb is there, written such that tests will be run in the context of the dummy app, booted over at ../dummy/config/environment.

I describe this because I think a lot of people don't know about this new generator, which sets things up so nicely.

But my question is, has anyone figured out how to do this with rspec instead? I have tried to follow the same principles DIY to set things up like this for rspec in a rails plugin gem, but am running into various confusing roadblocks, and am hoping maybe someone else has already figured it out (or would be interested in figuring it out for the rest of us, heh).


Source: (StackOverflow)

How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

I am doing the Rails Tutorial for the second time around and this time, when I enter this

rails generate integration_test static_pages

I get spec/rails_helper.rb and spec/spec_helper.rb instead of just spec/spec_helper.rb

Now when I run my tests, they are longer (more "verbose") and slower. I am wondering what the difference between the two files is, and if I did something wrong? Also, is there a way to get rid of the rails_helper.rb file without messing everything up?

Thanks, let me know if you need me to provide more code or info.


Source: (StackOverflow)

Circular Dependency Error in Rails 4 during Rspec Request Specs

I have an app running on Rails 4 / Ruby 2. The frontend is being developed with Ember.

When running rake spec, I'm getting the below exception. It seems to only happen in Rspec feature specs (not in dev mode). Also, it usually only happens if I run just the one feature spec; if I run the entire test suite, I don't usually get the error.

The error is occurring because of this commit to Rails: https://github.com/rails/rails/commit/b33700f5580b4cd85379a1dc60fa341ac4d8deb2

But, of course, I don't know if that's the real problem or if it's something deeper in the call stack. I do know, if I make a minimal change in the Rails code to not throw that error, everything seems to work fine and my tests pass. But something, somewhere, seems to be trying to load LocationsController even though it's already loaded.

Any help is greatly appreciated, as this one has me really stumped.

  1) Locations Creating locations
     Failure/Error: Unable to find matching line from backtrace
     RuntimeError:
       Circular dependency detected while autoloading constant LocationsController
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:460:in `load_missing_constant'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:183:in `const_missing'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:226:in `const_get'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:226:in `block in constantize'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:224:in `each'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:224:in `inject'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/inflector/methods.rb:224:in `constantize'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:534:in `get'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/dependencies.rb:565:in `constantize'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:76:in `controller_reference'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:66:in `controller'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:44:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/journey/router.rb:71:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/journey/router.rb:59:in `each'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/journey/router.rb:59:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/routing/route_set.rb:655:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/request_store-1.0.5/lib/request_store/middleware.rb:9:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/warden-1.2.1/lib/warden/manager.rb:34:in `catch'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/warden-1.2.1/lib/warden/manager.rb:34:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/etag.rb:23:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/conditionalget.rb:25:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/flash.rb:241:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/cookies.rb:486:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activerecord/lib/active_record/query_cache.rb:36:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/callbacks.rb:373:in `_run__1828229838678430325__call__callbacks'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/callbacks.rb:80:in `run_callbacks'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/rack/logger.rb:38:in `call_app'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/rack/logger.rb:21:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/tagged_logging.rb:67:in `block in tagged'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/tagged_logging.rb:25:in `tagged'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/tagged_logging.rb:67:in `tagged'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/rack/logger.rb:21:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/quiet_assets-1.0.2/lib/quiet_assets.rb:18:in `call_with_quiet_assets'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/activesupport/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/actionpack/lib/action_dispatch/middleware/static.rb:64:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/engine.rb:511:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/bundler/gems/rails-783c6711a4b4/railties/lib/rails/application.rb:97:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/builder.rb:138:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/urlmap.rb:65:in `block in call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/urlmap.rb:50:in `each'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/urlmap.rb:50:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/capybara-2.1.0/lib/capybara/server.rb:19:in `call'
     # /usr/local/rvm/gems/ruby-2.0.0-p195@nepco/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
     # /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
     # /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
     # /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

routes.rb:

get '/locations', to: 'main#index', :as => :em_locations

scope 'api' do
  resources :locations, only: [:index]
end

Relevant Gems:

  * capybara (2.1.0)
  * rspec (2.13.0)
  * rspec-core (2.13.1)
  * rspec-expectations (2.13.0)
  * rspec-mocks (2.13.1)
  * rspec-rails (2.13.2)
  * ember-data-source (0.0.5)
  * ember-rails (0.12.0)
  * ember-source (1.0.0.rc3.3)
  * active_model_serializers (0.8.1) 

What I've tried:

  • Changing the names and paths of my routes, thinking the ember /locations might be conflicting.
  • Changed a number of different Rails options I thought might be relevant. Actually, if I set config.cache_classes to false in the test.rb env file, it solves the problem albeit at the cost of making the specs run about 5X slower.

Here's the spec I'm running:

require 'spec_helper'

feature 'Locations', js: true, strategy: :truncation, slow: true do

  before do
    login_for_request
    Factory.create(:warehouse, code: "TW", name: 'TEST WAREHOUSE')
  end

  scenario "Creating locations" do
    visit em_locations_path
    click_link "create-locations"
    select 'TEST WAREHOUSE', from: "warehouse"
    select "GLD", from: "section"
    fill_in "row-start", with: "AA"
    fill_in "row-end", with: "AB"
    fill_in "bay-start", with: "1"
    fill_in "bay-end", with: "2"
    fill_in "tier-start", with: "1"
    fill_in "tier-end", with: "2"
    fill_in "subbay-start", with: "1"
    fill_in "subbay-end", with: "2"
    click_button "Create"
  end


end

Source: (StackOverflow)

Tracing memory leak in Ruby on Rails 3 / Postgres / Apache Passenger application

Hello,

we have recently updated an application to Rails 3.0.4 (3.0.5 on online devel server). Most of the changes from 2.3.10 to 3.0.4 were due to obsolete or outdated plugins and gems, and were solvable with relative ease. But one thing makes me go mad:

Every single web request, in development mode, causes the server process to allocate about 50-60 MB more memory than before. This memory is not freed after the request, at least not all of it. After 10-20 requests, every Ruby instance consumed over 500 MB of RAM, while our previous Rails 2.3.10 instances were rarely above 200 MB.

This makes it impossible to run our 1300 tests, because the devel machine's 4GB of RAM is filled before the end of the tests. It only happens in development mode with cache_classes = false. If I switch cache_classes to true, the Rails instances will consume about 200MB of memory, and then stay there. However, during tests, even with cache_classes = true, memory usage will grow.

I queried ObjectSpace and found out that with each request, about 3500 new Proc, up to 50'000 new Strings and 3000 new Hashes and Arrays are created and not freed. These strings (when dumped) contained my whole source code including plugins and gems, documentation, source code comments and path names. (Why?)

To find the cause for this, here's what I tried: (After every change, I hammered the apps with ab -n 50.)

  1. I created a fresh Rails 3 application with a single resource and controller and SQLite3 DB. Memory usage started at 60 MB and stayed below 80 MB.
  2. I changed 'sqlite3' to 'pg' and pointed the new Rails 3 app to my existing Postgres DB. Memory usage started at 110 MB and did not grow beyond 130MB. (Side question: Why does the Postgres gem use so much more memory than the SQLite3 gem?)
  3. I copied over my Gemfile and Gemfile.lock from the broken Rails3 app to the bare bones app and ran bundle install. No change, memory stayed at about 115MB, no matter how many requests were made.
  4. I created an empty "def FooController; def foo; render :text => 'foo' end; end" in the broken Rails3 app. Memory usage grew more slowly, but it still never stopped growing after requests.
  5. I deleted every route except for the FooController route. No change.
  6. I disabled all Gems except for the following: pg, rails, aasm, will_paginate, geokit-rails3, koala, omniauth, paperclip. No change.
  7. I disabled every before_filter and after_filter in ApplicationController and every nonessential include in environment.rb. I also synced boot.rb, environment.rb and application.rb with my bare-bones Rails 3 app, except for five relatively simple observers, autoloading files in /lib and filter_parameters. No change. Every new request still consumed an additional 10-50 MB of RAM.

If you have an idea what is going wrong here, and where the memory leak could be, I would really appreciate any help. I am running Rails 3.0.4 on OS X Snow Leopard, Rails 3.0.5 on Debian Lenny, and

Thank you!

Coming closer:

I have removed every plugin, every gem, every extension and everything that I did not personally write myself, so that my application is basically naked. Especially, I removed these plugins: acts_as_list, acts_as_tree, asset_packager, forgot_password, fudge_form, fudge_scaffold, paperclippolymorph, query_trace, rails_upgrade, repeated_auto_complete-0.1.0, role_requirement, to_select, validates_url, and ym4r_gm.

Now my application - only the above FooController still works! - starts up with 65MB and never goes beyond 75MB of RAM, even after hammering it with ab -n 1000 -c1 (1000 HTTP requests to /foo using ApacheBench). Unfortunately, without the plugins, this is also the only URI that works at all.

After some digging, it seems that a combination between the Restful Authentication and Acts As State Machine (AASM) plugins causes the memory leak. See also https://github.com/Satish/restful-authentication/issues#issue/11. I'm not sure yet why, and just doing "include AASM" in my bare-bones project does not cause RAM usage growth just by itself.

I will investigate further.

Culprit found

It is AASM. In Rails 3 it seems to leak AASM::xxx object instances. see

Second culprit found

There was another memory leak in rspec. This made my tests almost unbearably slow, even after removing AASM, because two parallel running rspec tasks (using https://github.com/grosser/parallel_tests) took almost 3GB of memory at the end. See https://github.com/rspec/rspec-core/issues/#issue/321.


Source: (StackOverflow)

Show runtime for each rspec example

currently I'm running more than 1k examples and it's taking a long time to complete (more than 20 minutes!!!).

I'd like to identify which examples are the ones taking more time to complete, is there any way to run rspec and return the time each example takes to complete(individually)? I'm using rspec 1.3.0 and rspec-rails 1.2.3


Source: (StackOverflow)

Why does upgrading to Rails 3.2.1 cause multiple Rspec tests to fail?

All 211 specs in my test suite were passing fine...until I upgraded from rails 3.2 to rails 3.2.1. Now 197 of my specs fail with errors. Most of these error have the "wrong number of arguments (0 for 1)" error described below.

Example #1:

class DocumentLibrary < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :slugged
  has_many :shelves, :dependent => :destroy
  has_many :documents, :through => :shelves
  validates :title, :presence => true, :uniqueness => true

  default_scope :order => :title
end

Spec:

  it "can be shown on the company menu" do
    dl = FactoryGirl.create(:document_library, :title => 'Test', :menu => false, :company => true)
    dl.should be_valid
  end

Fails with:

   1) DocumentLibrary can be shown on the company menu
     Failure/Error: dl = FactoryGirl.create(:document_library, title: 'Test', menu: false, company: true)
     ArgumentError:
       wrong number of arguments (0 for 1)
     # ./spec/models/document_library_spec.rb:6:in `block (2 levels) in <top (required)>'

If I place a call to the debugger before the the FactoryGirl.create line, I get:

/Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:252:
(rdb:1) c
/Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/core_ext/module/remove_method.rb:4: `' (NilClass)
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:249:in `set_it_up'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:200:in `subclass'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:187:in `describe'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/dsl.rb:18:in `describe'
    from /Users/Jason/code/rails/teamsite/spec/models/document_library_spec.rb:4:in `<top (required)>'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
    from /Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'
/Users/Jason/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.1/lib/active_support/core_ext/module/remove_method.rb:4:

Example #2

class Album < ActiveRecord::Base
  belongs_to :photo_library

  validates :title, :presence => true
  validates :title, :uniqueness => {scope: :photo_library_id}
end

class PhotoLibrary < ActiveRecord::Base
  default_scope :order => :title
  has_many :albums, :dependent => :destroy

  validates :title, :presence => true
  validates :title, :uniqueness => true
end

Spec:

  before :each do
    @photo_library = FactoryGirl.create(:photo_library, title: 'Products')
    login_admin
  end

  it "destroys an album" do
    3.times { FactoryGirl.create(:album, photo_library: @photo_library) }
    visit photo_library_path(@photo_library)
    find("h3").click_link 'Delete'
    find("#notice").should have_content("Album deleted successfully")
    Album.count.should eq 2
  end

Fails with:

1) Albums destroys an album
     Failure/Error: login_admin
     ArgumentError:
       wrong number of arguments (0 for 1)
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/albums_spec.rb:7:in `block (2 levels) in <top (required)>'

Line 8 in my sessions_controller is:

user = User.find_by_email(params[:email])

Inspecting the params at this point shows everything (including :email) is present as it should be.

Contrasting Example #3

This spec with FactoryGirl passes fine:

  it "is unique within a library" do
    pl = FactoryGirl.create(:photo_library, title: 'Products')
    pl2 = FactoryGirl.create(:photo_library, title: 'Competitors')
    a = FactoryGirl.create(:album, title: 'Gold Series', photo_library: pl)
    na = Album.create(photo_library_id: pl.id, title: 'Gold Series')
    na.should_not be_valid
    na.title = 'Cyclone'
    na.should be_valid

    na = Album.create(photo_library_id: pl2.id, title: 'Gold Series')
    na.should be_valid
  end

The factories are defined as follows:

  factory :document_library do
    sequence(:title) { |n| "Library Title#{n}" }
  end

  factory :photo_library do
    sequence(:title) { |n| "Photo Library Title#{n}" }
  end

  factory :album do
    sequence(:title) {|n| "Album#{n}"}
  end

If I comment out the FriendlyId lines from the DocumentLibrary model, Example #1 passes. I have no idea why that makes a difference.

However, some specs still don't pass. Consider the following model (which doesn't use FriendlyId) and spec that flunks in 3.2.1:

class DrawingLibrary < ActiveRecord::Base
  validates :title, :presence => true
  default_scope order: :title 
  has_many :drawings, :dependent => :destroy
end

 it "displays in alpha order" do
  FactoryGirl.create(:drawing_library, title: 'C')
  FactoryGirl.create(:drawing_library, title: 'B')
  FactoryGirl.create(:drawing_library, title: 'A')

  ts = ''

  DrawingLibrary.all.each do |draw_lib|
    ts += draw_lib.title
  end

  ts.should eq 'ABC'
end

1) DrawingLibrary displays in alpha order
     Failure/Error: DrawingLibrary.all.each do |draw_lib|
     ArgumentError:
       wrong number of arguments (0 for 1)
     # ./spec/models/drawing_library_spec.rb:19:in `block (2 levels) in <top (required)>'

Results of: rspec spec/models/document_library_spec.rb --backtrace

 1) DocumentLibrary can be shown on the company menu
 Failure/Error: dl = FactoryGirl.create(:document_library, title: 'Test', menu: false, company: true)
 ArgumentError:
   wrong number of arguments (0 for 1)
 # ./spec/models/document_library_spec.rb:6:in `block (2 levels) in <top (required)>'

I'm using rvm with ruby 1.9.3, with Rubygems at 1.8.16. Factory Girl is at 2.6.0, with factory_girl_rails at 1.7.0. rspec-rails is at 2.8.1.

Here's what I know so far:

  • Downgrading back to Rails 3.2.0 makes everything work again
  • Upgrading to Edge Rails does not fix the problem
  • Running edge versions of the rspec gems doesn't fix the problem
  • The app runs properly and as expected in development mode. Only tests seem to be affected.
  • Downgrading to ruby 1.9.2 doesn't fix the problem
  • Upgrading to ruby 1.9.3-p125 doesn't fix the problem
  • Changing from MySQL to SQLite for the test environment doesn't fix the problem
  • Downgrading factory_girl_rails to 1.6.0 or even 1.5.0 doesn't fix the problem
  • Using Factory.create(...) instead of Factory(...) doesn't fix the problem
  • Using FactoryGirl.define() and FactoryGirl.create() doesn't fix the problem
  • Commenting out the FriendlyId stuff makes some of the specs pass (see below)
  • Running db:test:prepare (or db:reset, db:migrate) does not fix the problem
  • Changing all FactoryGirl definitions/creations to be consistent doesn't fix the problem
  • Reinstalling gems under a new rvm gemset (or even reinstalling rvm entirely) doesn't fix the problem
  • Rewriting these tests in Test/Unit and FactoryGirl produces no errors. So FactoryGirl might not be the problem

Can anyone point me in a direction on this? Or offer troubleshooting advice?

Here's my Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.2.1'

gem 'mysql2'
gem 'dynamic_form'
gem 'friendly_id'

group :assets do
  gem 'sass-rails', "  ~> 3.2.3"
  gem 'coffee-rails', "~> 3.2.1"
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'therubyracer'
gem 'bcrypt-ruby'
gem 'capistrano'
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"

group :test, :development do
  gem 'rspec-rails'
  gem 'launchy'
  gem 'ruby-debug19'
  gem 'database_cleaner'
  gem 'capybara-webkit'
  gem 'spork', '~> 0.9.0.rc'
  gem 'guard-spork'
end

group :development do
  gem 'fuubar'
  gem 'powder'
end

group :test do
  gem 'turn', :require => false
  gem 'capybara'
  gem 'factory_girl_rails'
  gem 'guard-rspec'
end

The only differences in Gemfile.lock after upgrading to Rails 3.2.1 are with the Rails core libraries (no testing gems changed).


Source: (StackOverflow)

Capybara 2.1 Error uninitialized constant Rails (NameError)

I just upgraded my Capybara Gem from version 1 to 2.1.0 (latest). Based on Capybara Readme, I added this following lines to my spec_helper.rb inside Spork.prefork block

require 'capybara/rspec'
require 'capybara/rails'

But, I got an error

/home/user_1/.rvm/gems/ruby-1.9.3-p392/gems/capybara-2.1.0/lib/capybara/rails.rb:6:in `block (2 levels) in <top (required)>': uninitialized constant Rails (NameError)

Did I miss something in order capybara to work properly ?


Source: (StackOverflow)