Ruby Flare

Your awesome Tagline

Posts tagged rspec

12 notes

bypass_rescue in RSpec

If you are using rescue_from to handle an exception then the following spec won’t work:

it "should raise an access denied error" do
  expect { get(:new) }.to raise_error(CanCan::AccessDenied)
end

Instead you need to use rspec-rails’s bypass_rescue:

it "should raise an access denied error" do
  bypass_rescue
  expect { get(:new) }.to raise_error(CanCan::AccessDenied)
end

This works in rspec-rails 1.3 but from what I understand it wasn’t originally available in rspec-rails 2. It is about to make a comeback though.

Filed under rspec rails

7 notes

Clean rspec output

I like my rspec progress dots to remain undisturbed by output generated by the application eg puts, restarting/reindexing sphinx, prince. I use the silence_stream rails kernel method to keep things quiet:

silence_stream(STDOUT) { Users.import }

(Source: api.rubyonrails.org)

Filed under rspec

9 notes

Add cucumber to rake stats

I added the following to the statsetup task in rspec.rake inside my rails app:

::STATS_DIRECTORIES << %w( Cucumber\ features features ) if File.exist?('features')
::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')

Now when I run rake stats, I get:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  1970 |  1576 |      24 |     174 |   7 |     7 |
| Helpers              |   449 |   355 |       0 |      55 |   0 |     4 |
| Models               |  3589 |  2560 |      68 |     257 |   3 |     7 |
| Libraries            |   645 |   444 |      11 |      50 |   4 |     6 |
| Model specs          |  4850 |  4047 |       2 |      15 |   7 |   267 |
| View specs           |   476 |   409 |       0 |       0 |   0 |     0 |
| Controller specs     |  2880 |  2487 |       0 |      42 |   0 |    57 |
| Helper specs         |   331 |   303 |       0 |       0 |   0 |     0 |
| Library specs        |   144 |   126 |       0 |       0 |   0 |     0 |
| Cucumber features    |  2446 |  1737 |       1 |       9 |   9 |   191 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 17780 | 14044 |     106 |     602 |   5 |    21 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 4935     Test LOC: 9109     Code to Test Ratio: 1:1.8

(Source: github.com)

Filed under rails rspec rake stats

3 notes

Yielding multiple times

When you need to stub out the values you pass into a block you can use rspec’s and_yield method:

sftp_mock.stub_chain(:dir, :foreach).and_yield(entry_1)

When you need to pass in values several times you can chain the and_yield:

sftp_mock.stub_chain(:dir, :foreach).and_yield(entry_1).and_yield(entry_2)

These examples were for spec’ing the behaviour of downloading files from an sftp server where in my tests I’ve mocked out the sftp server (entry_1 and entry_2 are mock files). The code is something like this:

Net::SFTP.start('host', 'username', :password => 'password', :auth_methods => ['password']) do |sftp|
  sftp.dir.foreach('/results') do |entry|
    # do stuff
  end
end

In my tests, I mocked out the sftp with the following:

sftp_mock = mock('sftp')
Net::SFTP.stub(:start).and_yield(sftp_mock)

Filed under rspec sftp

1 note

Silence warnings

If you find that you need to reset the value of a constant in a spec, use the silence_warnings rails method to keep your output quiet:

silence_warnings { MY_CONSTANT = "hello" }

The times I find I need to modify a constant in a spec is when it is a constant that ought to be modified only for your tests eg a filepath or a pagination value.

(Source: api.rubyonrails.org)

Filed under rails rspec

1 note

Fail fast with RSpec

RSpec 2 gives you the ability to fail fast - your specs stop running as soon as an failure has been encountered.

Add this to your spec_helper.rb:

RSpec.configure do |c|
  c.fail_fast = true
end

If you don’t want to use fail fast all the time, activate it as required with the command line option:

bundle exec rspec spec --fail-fast

(Source: jeffkreeftmeijer.com)

Filed under rspec

0 notes

Making RSpec even more concise

Following on from a previous post, I gave the following rspec example:

describe “New User” do
  before(:each) do
    @user = User.new
  end
  it “should not be registered” do
    @user.should_not be_registered
  end
end

And made it more concise using the let() method:

describe “New User” do
  let(:user) { User.new }
  it “should not be registered” do
    user.should_not be_registered
  end
end

To make it even more concise and potentially more readable, we can instead use RSpec’s explicit subject which, once declared allows an example to delegate to that subject like so:

describe “New User” do
  subject { User.new }
  it { should_not be_registered }
end

When should() and should_not() have no explicit receiver the example will delegate them to the subject. And to take it even further, we can now use RSpec’s implicit subject:

describe User do
  it { should_not be_registered }
end

When there is no explicit subject, RSpec tries to imply a subject from the describe block. It takes User and creates a new instance of it for its subject eg User.new

(Source: rspec.info)

Filed under rspec testing