Tag Archives: RSpec

Selective stubbing of method calls in RSpec

While building a simple API in Rails, I had the following method in my User model that needed to be tested:

def generate_auth_token!
  loop do
    self.auth_token = Devise.friendly_token
    break unless self.class.exists?(auth_token: auth_token)
  end
end

The method uses Devise’s friendly_token method to generate an authentication token for a new user record. After calling the method once, the loop checks whether a user record with the same auth token already exists. If the answer is positive, the method goes for a second try; otherwise it breaks out of the loop, mission accomplished. Continue reading

Advertisements
Tagged , ,

RSpec 3.0.1 & RubyMine

After the recent RSpec update, warnings started showing up in RubyMine:

Deprecation Warnings: Treating `metadata[:execution_result]` as a hash is deprecated. Use the attributes methods to access the data instead. Called from …/teamcity/rspec3_formatter.rb: line#

(So, RSpec is complaining that RubyMine’s formatter is not accessing its data properly?) The warnings were overwhelming to the point of obscuring the results of running the test. Simple fix: Continue reading

Tagged , ,

Sometimes everyone needs a break

I’ve been working through – and enjoying – the EdX/Berkeley CS169 course for the past couple of months. With a bit of a break between the two halves, I thought I’d start a small Rails project of my own. After including ‘rspec-rails’ in my Gemfile, I ran bundle install and then

$ rails g rspec:install
Could not find generator rspec:install

Continue reading

Tagged , ,

Paperclip attachments in RSpec factories

Assuming one has a model whose attachments are handled by Paperclip, i.e.

$ rails generate paperclip user photo

app/models/user.rb:

class User < ActiveRecord::Base

  has_attached_file :photo
end

then to generate a test model with FactoryGirl, including the attachment: Continue reading

Tagged , , ,

Testing images with RSpec

To test whether the right image is displayed on a page:


expect(page).to have_css("img[src*='image_name.png']")

i.e. “Expect page to have an <img> tag whose src attribute contains the name of the required image.”

Tagged , ,

Avoid RSpec and Capybara naming conflicts

There I was, testing user authorization within the Posts controller of my blogging app, when I hit a wall:

describe 'authorization' do
  .
  .
  describe 'in the Posts controller' do
    .
    .
    describe 'attempting to issue a direct POST request without being signed in' do
      before { post posts_path }
      specify { expect(response).to redirect_to signin_path }
    end
  end
end

Simple. But it fails. It works just fine a few dozen lines above within the “Users controller” test block, but fails here. What gives?

Continue reading

Tagged , ,

From scratch: Rails Mailer, letter_opener, RSpec & Heroku

While Michael Hartl’s Rails Tutorial covers an awful lot of ground, it necessarily leaves some topics out. One of these is the Rails Action Mailer, which Hartl offers as a suggestion for further extension of the SampleApp. While resources for getting the Mailer to work are plentiful, they are rather decentralized, especially for the particular scenario described in this article’s title. Here, then, are the steps I needed to follow to get everything up and running. Continue reading

Tagged , , , ,