On Tuesday, 23 April 2019 11:32:45 UTC+2, belgoros wrote:
>
> Adding *before(:example)* hook to clear the cache seems to fix the 
> problem. Is it the right way to go?
>
> RSpec.describe "Addresses", type: :request do
>   let(:user)     { create(:user) }
>   let!(:address) { create(:address, shop: user.shop)}
>   let(:headers)  { valid_headers(user.username) }
>   
>   before(:example) { Rails.cache.clear }
>

I found the solution proposed by Thoughtbot 
<https://thoughtbot.com/blog/fragment-caching-in-tests>, - I added the 
following into my config/environments/test.rb:

config.cache_store = :null_store



and remove the line clearing Rails cache:

Rails.cache.clear



Now the failing example passes again:

it 'updates the modified attributes' do
  expect(address.reload.street).to eq 'new fancy street'
end




> On Tue, 23 Apr 2019 at 11:27, belgoros <[email protected]> wrote:
>
>> I'm using ActiveModel Serializers gem with Rails API and I tried to 
>> enable caching on `Address` following the docs of AMS 
>> <https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/caching.md>
>>  
>> as follows:
>>
>> #serializers/address_serializer.rb
>>
>>
>> # frozen_string_literal: true
>>
>>
>> class AddressSerializer < ActiveModel::Serializer
>>   cache key: 'address', expires_in: 3.hours
>>
>>
>>   attributes :city,
>>              :id,
>>              :latitude,
>>              :longitude,
>>              :modified_by,
>>              :postal_code,
>>              :region,
>>              :street,
>>              :updated_at
>>
>>
>>   belongs_to :shop
>> end
>>
>>
>>
>> But with this in place the previously passing request spec failed:
>>
>> #spec/requests/address_spec.rb
>>
>>
>> require 'rails_helper'
>>
>>
>> RSpec.describe "Addresses", type: :request do
>>   let(:user)     { create(:user) }
>>   let!(:address) { create(:address, shop: user.shop)}
>>   let(:headers)  { valid_headers(user.username) }
>>
>>
>> describe 'PATCH /shops/:shop_identifer/address' do
>>     let(:valid_params) do
>>       ams_json(
>>         Address,
>>         city: address.city,
>>         postal_code: address.postal_code,
>>         street: 'new fancy street',
>>         modified_by: address.modified_by,
>>         shop: address.shop,
>>         id: address.id
>>       )
>>     end
>>
>>
>>     before { patch "/shops/#{address.shop.identifier}/address", params: 
>> valid_params, headers: headers }
>>
>>
>>     it 'returns status code 204' do
>>       expect(response).to have_http_status(204)
>>     end
>>
>>
>>     it 'updates the modified attributes' do
>>       updated = Address.find(address.id)
>>       expect(updated.street).to eq 'new fancy street'
>>     end
>>   end
>> end
>>
>>
>>
>> with error:
>>
>> 1) Addresses PATCH /shops/:shop_identifer/address updates the modified 
>> attributes 
>>
>>  Failure/Error: expect(updated.street).to eq 'new fancy street' 
>>
>>  
>>
>>  expected: "new fancy street" 
>>
>>  got: "7285 Dicki Circle" 
>>
>>  (compared using ==) 
>>
>>  # ./spec/requests/addresses_spec.rb:42:in `block (3 levels) in <top 
>> (required)>'
>>
>>
>> What am I missing? Thank you.
>>
>>
>> -- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "rspec" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/rspec/0m457NKBWFE/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rspec/90835c10-2133-4acb-9619-34fc01f368c3%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/rspec/90835c10-2133-4acb-9619-34fc01f368c3%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/ef16f471-582d-479f-88d0-28f58be65634%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to