← Back to Upcase

Rspec API testing doesn't persist a model


(Alex Bush) #1

Hello fellow Rubyists,

today I was writing some api tests using rack-test gem and everything was well, I had my context blocks with before blocks creating some mockup data using FactoryGirl. World was bright and green :slight_smile:
But all of a sudden I stopped getting the results from one of my api controllers. It turned out that when I create one specific model object with FactoryGirl in the before block it doesn’t get stored/persisted to the test db.
I checked it with persisted? call and it returns true but when I call Model.all (right after creating it, even in before block) it gives me back an empty array(and this is why my api controller doesn’t return anything since it to gets an empty array).
Checking the dev environment instead of test gives the correct results plus my other models created with FactoryGirl don’t have such a problem.
Could someone give me a piece of advice on what it might be?

Here is the code I have in my spec(if it helps at all):

require ‘spec_helper’
`
describe ‘/api/v2/festivals/:id/faq’, :type => :api do
describe ‘index’ do
before(:each) do
festival1 = create :festival
festival2 = create :festival

  faq1 = create :faq
  faq2 = create :faq

  festival1.faqs << faq1
end

context 'faqs index per festival viewable by the api users' do
  let(:url) { "/api/v2/festivals/#{festival1.id}/faq.json" }

  it 'json' do
    get url

    last_response.status.should eql 200

    faqs = JSON.parse last_response.body

    faqs.any? do |faq|
      faq['id'] == faq1.id
    end.should be_true
    
    faqs.any? do |faq|
      faq['id'] == faq2.id
    end.should_not be_true
  end
end

end
end`

faq1 and faq2 in this case don’t get persistent in test environment but return true on .persisted? call.


(Dan Croak) #2

@alexbush Sorry to hear it. Could you provide more context by showing your spec/spec_helper.rb and spec/factories.rb files?

In the meantime, here’s a slightly refactored version:

describe '/api/v2/festivals/:id/faq', type: :api do
  it 'returns FAQs for the given festival' do
    festival = create(:festival)
    faq = create(:faq, festival: festival)
    create(:faq)

    get "/api/v2/festivals/#{festival.id}/faq.json"

    last_response.status.should eql(200)
    faqs = JSON.parse(last_response.body)
    expect(faqs.size).to eql(1)
    expect(faqs.first['id']).to eq(faq.id)
  end
end

(Alex Bush) #3

Here they are in this gist: https://gist.github.com/JayMarshal/5573840

Essentially Faq is a sublcass of Post which is in turn a model that uses wordpress posts table(wp_posts)… don’t ask me why and how… I’m that one unfortunate developer who has to deal with the stuff/spaghetti that the previous dev left. :frowning:


(Alex Bush) #4

Hello @all, I know this isn’t a stackoverflow and I probably should’ve posted my question there as well but please let me know if anyone has an idea what it might be. Or if you need more info/code from my side I’d be happy to post it here(used gist last time because this forum doesn’t like @ signs that much… or I probably just paste code in a wrong way…).

Thx.