Reputation: 444
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.
Upvotes: 1
Views: 421
Reputation: 444
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
Upvotes: 1