yfj score 12
2019/04/16 18:11 投稿
[Ruby on rails] rspecでNoMethodError。ActiveModelがスキーマ情報を認識してない? |
rspecを実行時のエラーの解決方法を知りたく、質問させていただきます。 |
調べた限りでrspec実行時に、activeModelがDBのスキーマ情報を読み取れてないのが原因だと思っています。 |
が、どのように修正したらいいのか分からず。。。 |
解決策をアドバイス頂けると幸いでございます。 |
※丸一日使っても解決できず。。。 |
**[事象]** |
rspec実行時に、noMethodErrorが発生する。 |
しかし、そのメソッド(というかenum)が確実に存在する |
**テストコード** |
```ruby |
~(省略)~ |
before do |
create(:product, user: user, indentify_route: :admin_identify, review_status: :in_progress) |
end |
it '商品が新規で作られない' do |
~(省略)~ |
end |
``` |
createした瞬間にエラーが起きています。 |
**発生するエラー内容** |
```RUby |
NoMethodError: |
undefined method `indentify_route=' for #<Product:0x00007ff0b75fafc8> |
``` |
一見すると、indentify_routeをモデルに定義していない |
**modelは下記** |
```ruby |
class Product < ApplicationRecord |
~(省略)~ |
enum indentify_route: { |
user_identify: 0, # ユーザ |
admin_identify: 1 # 管理画面 |
} |
``` |
**schema.rbは下記** |
```ruby |
create_table "products", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| |
~(省略)~ |
t.integer "indentify_route", default: 0 |
t.index ["province_id"], name: "index_ |
t.index ["user_id"], name: "index_ |
t.index ["province_id"], name: "index_products_on_province_id" |
t.index ["user_id"], name: "index_products_on_user_id" |
end |
``` |
controller側だと |
`product.new(indentify_route: :user_identify)`を実行したときにエラーは発生しませんが、 |
rspecで同じコードを実行するとNoMethodエラーが発生します。 |
またrspec実行時にステップ実行(binding.pry)を仕込んでコンソールでProduct.newすると |
``` |
pry> Product.new(indentify_route: :admin_identify) |
``` |
下記のエラーが出ます。 |
``` |
ActiveModel::UnknownAttributeError: unknown attribute 'indentify_route' for |
ActiveModel::UnknownAttributeError: unknown attribute 'indentify_route' for Product. |
``` |
なので、activeModelがidentify_routeというカラム?スキーマ情報?を認知してないように思います。 |
やった対応としては下記です。 |
- puma(アプリサーバ)再起動。 |
- mysql再起動。 |
- PC再起動 |
でも解消できません。 |
なにかヒントになるような回答でも大歓迎でございます。 |
詳しい方、どうかアドバイスをお願いいたします。 |
yfj score 12
2019/04/16 18:10 投稿
[Ruby on rails] rspecでNoMethodError。ActiveModelがスキーマ情報を認識してない? |
rspecを実行時のエラーの解決方法を知りたく、質問させていただきます。 |
調べた限りでrspec実行時に、activeModelがDBのスキーマ情報を読み取れてないのが原因だと思っています。 |
が、どのように修正したらいいのか分からず。。。 |
解決策をアドバイス頂けると幸いでございます。 |
※丸一日使っても解決できず。。。 |
**[事象]** |
rspec実行時に、noMethodErrorが発生する。 |
しかし、そのメソッド(というかenum)が確実に存在する |
**テストコード** |
```ruby |
~(省略)~ |
before do |
create(: |
create(:product, user: user, indentify_route: :admin_identify, review_status: :in_progress) |
end |
it '商品が新規で作られない' do |
~(省略)~ |
end |
``` |
createした瞬間にエラーが起きています。 |
**発生するエラー内容** |
```RUby |
NoMethodError: |
undefined method `indentify_route=' for #<Product:0x00007ff0b75fafc8> |
``` |
一見すると、indentify_routeをモデルに定義していない |
**modelは下記** |
```ruby |
class Product < ApplicationRecord |
~(省略)~ |
enum indentify_route: { |
user_identify: 0, # ユーザ |
admin_identify: 1 # 管理画面 |
} |
``` |
**schema.rbは下記** |
```ruby |
create_table "products", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| |
~(省略)~ |
t.integer "indentify_route", default: 0 |
t.index ["province_id"], name: "index_identifications_on_province_id" |
t.index ["user_id"], name: "index_identifications_on_user_id" |
end |
``` |
controller側だと |
`product.new(indentify_route: :user_identify)`を実行したときにエラーは発生しませんが、 |
rspecで同じコードを実行するとNoMethodエラーが発生します。 |
またrspec実行時にステップ実行(binding.pry)を仕込んでコンソールでProduct.newすると |
``` |
pry> Product.new(indentify_route: :admin_identify) |
``` |
下記のエラーが出ます。 |
``` |
ActiveModel::UnknownAttributeError: unknown attribute 'indentify_route' for Identification. |
``` |
なので、activeModelがidentify_routeというカラム?スキーマ情報?を認知してないように思います。 |
やった対応としては下記です。 |
- puma(アプリサーバ)再起動。 |
- mysql再起動。 |
- PC再起動 |
でも解消できません。 |
なにかヒントになるような回答でも大歓迎でございます。 |
詳しい方、どうかアドバイスをお願いいたします。 |