...speed up your tests. I found that deferring garbage collection would speed up my RSpec examples by about 15%, but it probably depends on the nature of your tests. I...
...Test::Unit in his example, here is how to apply the same trick to RSpec. First, copy this class to spec/support/deferred_garbage_collection.rb: class DeferredGarbageCollection DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC...
Shared example groups are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them. I recommend storing them like this...
...specs, put the following into your spec_helper.rb (for rails 4 in rails_helper.rb), above the RSpec.configure block: Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].each {|f| require f} Dir[Rails.root.join("spec/controllers/shared_examples/**/*.rb")].each {|f...
We are maintaining some vintage projects with tests written in Test::Unit instead of RSpec. Mocks and stubs are not features of Test::Unit, but you can use the Mocha...
...is a quick crash course to using mocks and stubs in Mocha, written for RSpec users: |---------------------------------------------------------| | RSpec | Mocha | |---------------------------------------------------------| | obj = double() | obj = mock() | | obj.stub(:method => 'value') | obj.stubs(:method).returns('value') |
result.should == 'Hello multiverse' end end end The reason why this works is because RSpec will evaluate the block assigned for the let call only when result is actually called...
RSpec 3.0 deprecates the :should way of writing specs for expecting things to happen. However, if you have tests you cannot change (e.g. because they are inside a gem, spanning...
...of Rails and RSpec), you can explicitly allow the deprecated syntax. Fix Inside spec/spec_helpber.rb, set rspec-expectations’ and/or rspec-mocks’ syntax as following: RSpec.configure do |config| # ... config.mock_with :rspec do...
...MySQL lamers, here is a cheat sheet for Jasmine when you're used to RSpec. Note that Jasmine syntax has changed with Jasmine 2, so if you're using Jasmine...
...x you might instead want to use an older cheat sheet. Expectations # RSpec expect(foo).to.eq("value") expect(foo).to_not eq("value") # Jasmine expect(foo).toBe("value")
You know that Devise offers RSpec test helpers for controller specs. However, in request specs, they will not work. Here is a solution for request specs, adapted from the Devise...
...resource_or_scope) logout(scope) end end Finally, load that module for request specs: RSpec.configure do |config| config.include DeviseRequestSpecHelpers, type: :request end Done. You can now write request specs like...
...spec that will be implemented later as pending, include a failing spec body or RSpec 3 will consider the pending test as failing. The reasoning is: If the spec is...
...something that you want to keep, you may add fail to cause an error. RSpec will then consider the spec pending. it 'does something' do pending # This will be interesting...
I encountered a bug in RSpec 1.x where stubbed class methods ("static methods") would not be unstubbed before the next example, causing it to fail. This behavior can come...
...go as you edit your specs, since this can change the order in which RSpec evaluates your .rb files. I was not able to find a fix for this behavior...
...is run every time a @foo tagged scenario is hit" end You can tag RSpec examples like this: it 'does something', :foo => true do ... end What you need is the...
...following within the RSpec.configure do |config| block within spec_helper.rb config.before(:each, :foo => true) do puts "This is run every time a tagged rspec example is hit"
If rspec hangs with no output and you dont get a backtrace neither with --backtrace nor by just killing it with crtl-c, you can put the following in your...
...spec/spec_helper.rb: puts "rspec pid: #{Process.pid}" trap 'USR1' do threads = Thread.list puts puts "=" * 80 puts "Received USR1 signal; printing all #{threads.count} thread backtraces." threads.each do |thr| description = thr == Thread.main ? "Main thread...
While RSpec 1 and 2 decided that specs inside spec/model are model specs, and those inside spec/features are feature specs (and so on), RSpec 3 will no longer do that...
describe '...', type: 'feature' do # ... end Add this to your spec_helper.rb (inside the RSpec.configure block) to restore the old behavior: config.infer_spec_type_from_file_location...
What do we expect from the custom finder? We expect that it should find assets A, B, C and should...
Don't write resources :people, :concerns => :trashable Write resources :people do concerns :trashable end Why Writing a controller...
...Ruby code that prints something to the terminal, you can test that output. Since RSpec 3.0 there is a very convenient way to do that. Anything that writes to stdout...
Running projects parallel makes some trouble with PDF generation. Use geordi rspec spec to force sequential tests for the whole application or failed specs only. geordi rspec RTeX::Document::GenerationError...
RSpec supports a one-liner syntax for setting an expectation on the subject: describe Array do describe "when first created" do it { should be_empty } end end The example description...
...it should be empty" will be defined automatically. With RSpec 3's expect-based syntax you use it_is_expected instead: describe Array do describe "when first created" do
RSpec::Matchers.define :be_naturally_sorted do match do |array| array == array.natural_sort end end See RSpec: Where to put custom matchers and other support code. To use natural_sort, see...
RSpec's context (which is basically an alias for describe) takes over your whole application. No object may have its own context method, or you will always receive errors like...
RSpec's be_false behaves unexpectedly: nil.should be_false # passes, as the expectation returns true If you want to check for false, you need to do it like this:
# fails as expected Wat? See also RSpec: be_true does not actually check if a value is true
If you stub a method or set expectations with should_receive these stubbed methods may also yield blocks. This is...
rspec_spinner is a progress bar for RSpec which outputs failing examples as they happen (instead of all at the end). Installation gem install rspec_spinner Usage script/spec -r rspec...
...spinner -f RspecSpinner::Bar -c To make a shortcut in your .bashrc alias ss='script/spec -r rspec_spinner -f RspecSpinner::Bar -c' There's also an alternate runner RSpecSpinner::Spinner...
There seems to be no built-in matcher in RSpec to check if a string contains terms in the desired order. A simple workaround is to use a regular expression...