Undescriptive error when running spec, occurs dependent on seed

核能气质少年 提交于 2019-12-23 05:11:45

问题


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:

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 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

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


回答1:


I think you're on the right track that its related to saving the properties to your hstore data type as a prepared statement. Perhaps this answer will help you. :)

https://stackoverflow.com/a/11074481/793330




回答2:


try this out:

comment out the line below from spec_helper.rb if present.

config.order = "random"

And use database cleaner gem to fix this issue,as this issues seems to be with data with which your test suite is running.




回答3:


I have the same error in my Rails app. Finally I found that it was caused by the following code (using the active_model_serializers gem to serialize all attributes of User):

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 haven't found the real reason behind it yet. Hope it helps anyway.



来源:https://stackoverflow.com/questions/21555967/undescriptive-error-when-running-spec-occurs-dependent-on-seed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!