【问题标题】:rspec testing subclass sti methods rails 4rspec测试子类sti方法rails 4
【发布时间】:2016-07-07 19:43:16
【问题描述】:

我正在尝试为 sti 子类上的方法构建 rspec 测试,并且该测试仅读取父模型的方法。该方法在应用程序中有效,只是在 rspec 测试中无效。我无法弄清楚我错过了什么

models/animals/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)>'

更新

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

根据建议,我在测试中添加了以下行

expect(cat_mammal_animal.class.constantize).to eq(CatMammalAnimal)

得到了这个错误

1) MammalAnimal.favorite 和 .favorite 显示最喜欢的猫 失败/错误:expect(cat_animal_mammal.class.constantize).to eq(CatMammalAnimal)

 NoMethodError:
   undefined method `constantize' for #<Class:0x007f8ed4b8b0e0>
   Did you mean?  constants

【问题讨论】:

  • 当您将expect(cat_mammal_animal.class.constantize).to eq(CatMammalAnimal) 添加到您的期望值之上时会发生什么?你也可以发布你的工厂吗?
  • 我已经更新了它,但我不确定你想用那条新线测试什么。
  • 哎呀,我猜是常量化位。我的理论是您的工厂以某种方式创建了具有正确类型但错误类的对象。你能把常量化部分拿出来,把 CatMammalAnimal 变成一个字符串吗?

标签: ruby-on-rails ruby-on-rails-4 rspec single-table-inheritance


【解决方案1】:

我认为与其使用trait 来创建子类的对象,不如为这些对象创建单独的工厂。

factory :animal do
  name 'dragon'
end

factory :mammal, class: MammalAnimal do
  name 'zebra'
end

factory :cat, class: CatMammalAnimal do
  name 'calico'
end

所有这些都可以在animals.rb中定义

然后你可以像这样创建你的对象

create(:animal)
create(:mammal)
create(:cat)

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多