jsonapi-rb / jsonapi-rspec

RSpec matchers for JSON:API spec
https://rubygems.org/gems/jsonapi-rspec
MIT License
123 stars 24 forks source link

How to work `have_attributes`? #1

Closed artem-galas closed 7 years ago

artem-galas commented 7 years ago

I don't understand how should have_attributes method work. I am getting such json[‘data’]

{"id"=>"1", "type"=>"users", "attributes"=>{"id"=>1, "email"=>"colton_adams@dickinson.biz", "first_name"=>"Beaulah", "last_name"=>"Nitzsche", "company"=>"Balistreri and Sons"}}

but I get such error when I run test -

Failure/Error: expect(json['data']).to(have_attributes(:full_name, :email))
       expected {"id"=>"1", "type"=>"users", "attributes"=>{"id"=>1, "email"=>"colton_adams@dickinson.biz", "first_name"=>"Beaulah", "last_name"=>"Nitzsche", "company"=>"Balistreri and Sons"}} to have attributes :full_name and :email

My spec_helper.rb


require 'rails_helper'
require 'rspec-rails'

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.include Requests::JsonHelpers
  config.include JSONAPI::RSpec
end

My file_spec.rb


require 'rails_helper'

describe Api::V1::Devise::SessionsController do
  let!(:user) {FactoryGirl.create(:user)}

  describe 'POST /auth/sign_in' do
    it 'Success authorize user' do
      post '/api/v1/auth/sign_in', params: {email: user.email, password: user.password}
      expect(json['data']).to(have_attributes(:full_name, :email))
    end
  end
end
beauby commented 7 years ago

Your spec is failing because your payload does not have a full_name attribute (it has first_name and last_name).

artem-galas commented 7 years ago

@beauby Thanks for your reply. Yes, I have been mistaken, sorry :( Other issue - after adding 'jsonapi-rspec' gem into Gemfile I can't run rails routes/ rails db:migrate or any other rails task. I am getting the next error

rails routes
rails aborted!
NameError: uninitialized constant RSpec::Matchers
/Users/artemgalas/.rvm/gems/ruby-2.4.1@easylead/gems/jsonapi-rspec-0.0.2/lib/jsonapi/rspec/id.rb:4:in `<module:Id>'
/Users/artemgalas/.rvm/gems/ruby-2.4.1@easylead/gems/jsonapi-rspec-0.0.2/lib/jsonapi/rspec/id.rb:3:in `<module:RSpec>'
/Users/artemgalas/.rvm/gems/ruby-2.4.1@easylead/gems/jsonapi-rspec-0.0.2/lib/jsonapi/rspec/id.rb:2:in `<module:JSONAPI>'
/Users/artemgalas/.rvm/gems/ruby-2.4.1@easylead/gems/jsonapi-rspec-0.0.2/lib/jsonapi/rspec/id.rb:1:in `<top (required)>'
/Users/artemgalas/.rvm/gems/ruby-2.4.1@easylead/gems/jsonapi-rspec-0.0.2/lib/jsonapi/rspec.rb:1:in `<top (required)>'