I have written a feature spec to test for correct multitenancy behaviour within my application. It signs in as two different users, creates a new Presentation
by filling in a form and submitting it as each user, and confirms every time that only the tenant's own Presentations are visible to them.
This spec passes fine sometimes, and sometimes doesn't. What is really confusing is the error that occurs:
and that is not a error at pasting, it literally says can't cast ActiveSupport::HashWithIndifferentAccess to
and just stops there. The referenced lines in context:
./app/controllers/presentations_controller.rb
18 def create 19 @presentation = Presentation.new(presentation_params) 20 21 if @presentation.save 22 flash_for(@presentation, :create) 23 redirect_to @presentation 24 else 25 render :new 26 end 27 end
./spec/features/presentation_management_spec.rb
and I don't see how that error makes any sense here, I'm just clicking a button that will create a new Presentation and save it. The saving itself throws the error. How? Why?
To make it worse, this only occurs with some rspec seeds. For instance, 48719
runs fine:
.......................................................................................... Finished in 48.17 seconds 90 examples, 0 failures Randomized with seed 48719
Which means that this error occurs only with some execution orders of the specs. But the error isn't descriptive at all, and I don't know what to do. I've tried resetting all databases, restarting my entire server, printing out the page where the button is pressed, commenting different lines out, and nothing would change, or even hint at what might be wrong.
Here's my Presentation model:
class Presentation < ActiveRecord::Base store_accessor :properties, :bg_color, :bg_image validates :title, presence: true validates :default_duration, presence: true, numericality: { greater_than_or_equal_to: 1 } def to_s title end end
EDIT: I checked the test logs and found this in a failing case:
SQL (1.3ms) INSERT INTO "presentations" ("created_at", "default_duration", "properties", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Tue, 04 Feb 2014 20:56:26 UTC +00:00], ["default_duration", 60.0], ["properties", {"bg_ color"=>"#ffffff"}], ["title", "Example Presentation yo"], ["updated_at", Tue, 04 Feb 2014 20:56:26 UTC +00:00]] TypeError: can't cast ActiveSupport::HashWithIndifferentAccess to : INSERT INTO "presentations" ("created_at", "default_duration", "properties", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" (1.6ms) ROLLBACK TO SAVEPOINT active_record_1 Completed 500 Internal Server Error in 10ms
and the only Hash here would be {"bg_color"=>"#ffffff"}
, assigned to properties
. The properties
field is an hstore
type. Maybe this is an issue with hstore, which might explain why the Rails error output was inable to put anything behind the can't cast ... to
, because it's not a data type Rails truly natively understands?
EDIT 2:
I just now got the exact same error in development while trying to create a new Presentation
on the rails console. My initial thought was that I had maybe left out the hstore
schema in the search_path
, but adding it did no good.
This confirms that it's not an issue of how the tests are written, but an actual coding problem. I'm not sure what to do at this point.