1

I've got issue during controller testing in rspec. I created an entity (stored in postgres) and passed the entity.id as HTTP request parameter. I can see that the entity was stored into DB within the test, but controller did not see it.

All DB request happened within one transaction, so I thought it will be the problem.

I've got bunch of similar tests, so there is no problem with the DB connection and anything like that.

I tried to move create :entity to before block, and loaded it to request as Entity.all.first.id. Using let!(:entity) {create :entity} was also useless. Taking it out from contexts too.

// notifications_controller_spec.rb
context 'with an entity' do
  it 'renders the list of notifications' do
    entity = create :entity

    pp Entity.all // returns array with stored entity

    get :index, entity: entity.id

    expect(response.status).to eq 200
    expect(response).to render_template 'index'
  end
end



//notifications_controller.rb
def index

  pp Entity.all // returns empty array

  defaults_entity
  return render 'welcome_no_entity' if @entity.nil?

  @notifications = NotificationDecorator.decorate_collection(
    Notification::EntityScopedQuery.
      new(@entity.id, { order: { created_at: :desc } }).
      all
  )
 end

I would expect to see stored the same entity as in the test.

There will be probably some stupid mistake. Thx for help.

EDIT1:

// create entity
INSERT INTO "entities" ("name", "type", "customer_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["name", "test"], ["type", "complex"], ["customer_id", 514911], ["created_at", "2019-01-24 08:43:00.145471"], ["updated_at", "2019-01-24 08:43:00.145471"]]
// select in test
SELECT "entities".* FROM "entities"

There is no other log like SELECT "entities".* FROM "entities", even when there should be. I can also say, select is from the test.

4
  • Is entity in your test properly persisted? entity.valid? && entity.persisted? Commented Jan 24, 2019 at 8:37
  • In defaults_entity: @entity = params.deep_symbolize_keys[:entity]. But it is just an ID. Using that ID, entity is loaded from DB. And that is the problem. There is not entity stored in the DB on controllers site. Commented Jan 24, 2019 at 8:38
  • Actually, ID is passed ok, strange. I couldn't be passed when the entity was not created before Commented Jan 24, 2019 at 8:39
  • pp entity.valid? && entity.persisted? // true Commented Jan 24, 2019 at 8:40

1 Answer 1

1

The problem is, that we use gem Multitenant within the controller. So in the controller it does not make select like SELECT * FROM entities WHERE id = ?, but SELECT "entities".* FROM "entities" WHERE "entities"."customer_id" = $1

And customer_id was not defined, so it was totally random.

Fixed in test: entity = create :entity, customer_id: customer_id

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, multi-tenant app. Critical detail. :)
Yup, sorry, I did not even know about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.