Accessing controller instance variables from within an rspec controller spec

Use the assigns method (*note: assigns is now deprecated. See bottom of my answer for info): it “assigns the @widget” expect(assigns(:widget)).not_to be_nil end Of course, you can inspect widget as you’d like, but without seeing what @widget is from your provided controller code, I just checked if it was nil If you’re wanting to puts … Read more

Show runtime for each rspec example

You can use profiling to list your 10 slowest examples: spec -p spec/*/*_spec.rb –colour –format profile If you run this on a specific test suite you can get the 10 slowest examples from a smaller subset of examples: spec -p spec/models/user_spec.rb –format profile Newer Versions of Rspec For newer versions of rspec you should use … Read more

mock Rails.env.development? using rspec

There is a much better way described here: https://stackoverflow.com/a/24052647/362378 it “should do something specific for production” do allow(Rails).to receive(:env) { “production”.inquiry } #other assertions end This will provide all the functions like Rails.env.test? and also works if you just compare the strings like Rails.env == ‘production’

Testing Rails 3.1 mountable engine with Rspec

I am using RSpec with a Rails engine without issues. I created my plugin using the following switches: -T –full –dummy-path=spec/dummy. -T excludes test/unit –full indicates that the plugin is an engine –dummy-path is simply so that we don’t get a test directory (the default is test/dummy). From there I used the spec_helper from the … Read more

Rspec/Capybara loading in progress, circular require considered harmful

Remove –warnings from .rspec. The generator in rspec 3.0.0 included this setting, but we’ve realized it was a mistake — while it’s good to encourage users to write warning-free code, it’s confusing for users to get these warnings without being sure why. For more info: https://github.com/rspec/rspec-core/issues/1571

Rails 3.1 plugin gem, dummy test app, rspec

Create the plugin without test-unit and specify the path for the dummy application: rails plugin new foobar –skip-test-unit –dummy-path=spec/dummy Add rspec-rails as a development dependency to the gemspec file (foobar.gemspec): Gem::Specification.new do |s| . . . s.add_development_dependency “rspec-rails” end Run bundle install Create a symlink from the dummy app to the plugin spec directory and … Read more

Capybara tests with :js=>true… Routing Error: No route matches [GET] “/assets”

Your missing route looks like some auto-generated assets path with nil input. Are you sure your assets are correctly specified in your css/sass or wherever this route comes from? I have been struggling with a similiar issue where an asset path induced a routing error 1) user signup: [ JS ] : creates User on … Read more

How can I test :inclusion validation in Rails using RSpec

Use shoulda_matchers In recent versions of shoulda-matchers (at least as of v2.7.0), you can do: expect(subject).to validate_inclusion_of(:active).in_array(%w[Y N]) This tests that the array of acceptable values in the validation exactly matches this spec. In earlier versions, >= v1.4 , shoulda_matchers supports this syntax: it {should ensure_inclusion_of(:active).in_array(%w[Y N]) }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)