thoughtbot / ios-on-rails

A guide to building a Rails API and iOS app
Other
76 stars 6 forks source link

Update to specs #114

Open danielfriis opened 9 years ago

danielfriis commented 9 years ago

In the new version of rspec spec_helper has been split into two, and you should now require rails_helper instead in the top of all specs.

Also, in the spec below, you should call .as_json on event.ended_at, as you do with event.started_at, because both are converted to json.

# spec/requests/api/v1/events/events_spec.rb

require 'spec_helper'

describe 'GET /v1/events/:id' do
    it 'returns an event by :id' do
      event = create(:event)

      get "/v1/events/#{event.id}"

      expect(response_json).to eq( {

        'address' => event.address,
        'ended_at' => event.ended_at,
        'id' => event.id,
        'lat' => event.lat,
        'lon' => event.lon,
        'name' => event.name,
        'started_at' => event.started_at.as_json,
        'owner' => {
          'id' => event.owner.id
        }
      } )
    end
end
theresaluu commented 8 years ago

I wasn't sure where in the book or sample app to add this note so instead of a PR, I decided to add to this issue: I was getting Shoulda Matchers errors stating it didn't recognize it {should validate_presence_of(:lat)} The solution is when you make your rails_helper.rb file, you have to manually install Shoulda-matchers in my testing framework (it used to be installed automatically). For reference, my rails_helper.rb is as follows- the Shoulda-Matchers install info is at the bottom:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'webmock/rspec'
require 'shoulda/matchers'

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.expect_with :rspec do |c|
    c.syntax = :expect
  end
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.infer_spec_type_from_file_location!
  config.fail_fast = true
  config.infer_base_class_for_anonymous_controllers = false
  config.use_transactional_fixtures = false

  config.filter_rails_from_backtrace!
end

WebMock.disable_net_connect!(allow_localhost: true)

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :spec
    with.library :rails
  end
end