私はstiサブクラスのメソッドのrspecテストを構築しようとしていますが、テストでは親モデルのメソッドしか読み込まれません。メソッドはアプリで動作しますが、rspecテストでは動作しません。私はrspecテストサブクラスstiメソッドのレール4
モデル/動物/ animal.rb
class Animal < ActiveRecord::Base
def favorite
"unicorn"
end
end
モデル/動物/ mammal_animal.rb
class MammalAnimal < Animal
def favorite
"whale"
end
end
モデル/動物/ cat_mammal_animal.rbを欠けているかを把握することはできません
class CatMammalAnimal < MammalAnimal
def favorite
"tabby"
end
end
mammal_animal_spec.rb
提案どおりrequire 'rails_helper'
RSpec.describe MammalAnimal, type: :model do
let(:cat_mammal_animal) {FactoryGirl.create(:cat_factory)}
subject(:model) { cat_mammal_animal }
let(:described_class){"MammalAnimal"}
describe "a Cat" do
it "should initialize successfully as an instance of the described class" do
expect(subject).to be_a_kind_of described_class
end
it "should have attribute type" do
expect(subject).to have_attribute :type
end
it "has a valid factory" do
expect(cat_mammal_animal).to be_valid
end
describe ".favorite " do
it 'shows the favorite Cat' do
expect(cat_mammal_animal.type).to eq("CatMammalAnimal")
expect(cat_mammal_animal.favorite).to include("tabby")
expect(cat_mammal_animal.favorite).not_to include("whale")
expect(cat_mammal_animal.favorite).not_to include("unicorn")
print cat_mammal_animal.favorite
end
end
end
end
エラー
Failures:
1) MammalAnimal.favorite and .favorite shows the favorite Cat
Failure/Error: expect(cat_mammal_animal.type).to include("tabby")
expected "unicorn" to include "tabby"
# ./spec/models/mammal_animal_spec.rb:82:in `block (3 levels) in <top (required)>'
UPDATE
animals.rb
FactoryGirl.define do
factory :animal do
type 'Animal'
name "dragon"
trait :mammal do
type 'MammalAnimal'
name "zebra"
end
trait :cat do
type 'CatMammalAnimal'
name "calico"
end
factory :mammal_factory, traits: [:mammal]
factory :cat_factory, traits: [:cat]
end
end
、Iは、試験に
を以下の行を追加した期待(cat_mammal_animal .class.constantize).to eq(CatMa期待(cat_animal_mammal.class.constantize).TO EQ(CatMammalAnimal)
NoMethodError:
undefined method `constantize' for #<Class:0x007f8ed4b8b0e0>
Did you mean? constants
:mmalAnimal)
と、このエラー
1)MammalAnimal.favoriteと.favoriteだが好きな猫 失敗/エラーを示してい
のようなあなたのオブジェクトを作成することができ
animals.rb
に定義することができます(CatMammalAnimal )失敗した行の上のあなたの期待に?また、あなたの工場を投稿することはできますか? –
私はそれを更新しましたが、その新しい行でどのようなテストをしたいのかよくわかりません。 – NothingToSeeHere
私は定数ビットを推測します。私の理論は、あなたの工場はどういうわけか、正しいタイプのオブジェクトを作成していたが間違ったクラスだったということだった。定数部分を取り出してCatMammalAnimalを文字列にすることはできますか? –