Undetectable error when running spec, occurs depending on the seed

I wrote a function specification to test the correct layering behavior in my application. He subscribes as two different users, creates a new Presentation , filling out the form and sending it as every user, and confirms each time they see only their own documents for tenants.

This specification sometimes runs fine, and sometimes not. What really confuses the error:

 Failure/Error: click_button "Präsentation erstellen" TypeError: can't cast ActiveSupport::HashWithIndifferentAccess to # ./app/controllers/presentations_controller.rb:21:in `create' # ./spec/features/presentation_management_spec.rb:22:in `block (3 levels) in <top (required)>' 

and this is not an error when pasting, it literally says can't cast ActiveSupport::HashWithIndifferentAccess to and just stops there. Related strings in context:

./application/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 

./specifications/features/presentation_management_spec.rb

 16 sign_in @user1 17 18 visit new_presentation_path 19 fill_in "Titel", with: title1 20 fill_in "Standard-Foliendauer", with: "60" 21 22 click_button "Präsentation erstellen" 23 24 page.should have_content "erfolgreich erstellt" 25 page.should have_content title1 

and I don’t see how this error makes sense here, I just click on the button that will create a new presentation and save it. Salvation itself causes an error. How? Why?

To make this worse, this only happens with some rspec seeds . For example, 48719 works fine:

 .......................................................................................... Finished in 48.17 seconds 90 examples, 0 failures Randomized with seed 48719 

This means that this error only occurs with certain ordering specifications. But the error is not descriptive at all, and I do not know what to do. I tried to reset all databases, restart my entire server, print the page on which the button was clicked, comment on different lines and nothing will change, or even hint at something that might be wrong.

Here is my view 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 case of failure:

  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 will be {"bg_color"=>"#ffffff"} assigned by properties . The properties field is an hstore type. Maybe this is a problem with hstore, which may explain why the output of the Rails error was unable to put something behind can't cast ... to , because it is not a data type that Rails really understands initially?

EDIT 2 :

I just got the same development error while trying to create a new Presentation in the rails console. My initial thought was that I could exclude the hstore scheme in search_path , but adding it didn't help.

This confirms that this is not a problem of how the tests are written, but the encoding problem itself. I am not sure what to do at this moment.

0
source share
3 answers

I think that you are on the right track with saving properties for your hstore data type as a prepared statement. Perhaps this answer will help you. :)

stack overflow

+1
source

try it:

write the line below from spec_helper.rb, if any.

 config.order = "random" 

And use a database cleaner to fix this problem, as these problems seem to be related to the data your test suite is working with.

0
source

I have the same error in a Rails application. Finally, I found that this is caused by the following code (using the active_model_serializers gem to serialize all User attributes):

 class UserSerializer < ApplicationModelSerializer attributes *User.attribute_names.map(&:to_sym) end 

I changed the code to the following and then there were no errors:

 class UserSerializer < ApplicationModelSerializer def attributes object.attributes end end 

I have not yet found a real reason. Hope it helps anyway.

0
source

All Articles