首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Rspec解决问题失败/错误: let(:currency) { create(:currency) }

如何使用Rspec解决问题失败/错误: let(:currency) { create(:currency) }
EN

Stack Overflow用户
提问于 2021-03-06 08:29:48
回答 1查看 23关注 0票数 0

我正在使用rspec和factory Girl测试模型

我的模型

代码语言:javascript
复制
class Currency < ActiveRecord::Base
  has_many :countries

  validates :name, presence: true
  validates :name, uniqueness: true

  before_destroy :safe_to_delete

  def safe_to_delete
    countries.any? ? false : true
  end
end

我的工厂女孩

代码语言:javascript
复制
FactoryGirl.define do
  factory :currency, class: 'Currency' do
    sequence(:name) { |i| "Currency-#{i}" }
  end
end

我的currency_spec.rb是

代码语言:javascript
复制
require 'rails_helper'

describe Currency , type: :model do
  let(:currency) { create(:currency) }
  let(:currency1) { create(:currency) }
  let(:country) { create(:country) }

  describe 'associations' do
    subject {currency}
    it { should have_many(:countries) }
  end

  describe 'validations' do
    subject {currency}
    it { should validate_presence_of(:name) }
    it { should validate_uniqueness_of(:name) }
  end

  describe 'method save_to_delete' do
    context 'case false' do
        before { country.update_column(:currency_id, currency.id)  }
        subject { currency.destroy }
        it { is_expected.to be_falsy }
    end
    context 'case true' do
        before { country.update_column(:currency_id, currency1.id)  }
        subject { currency.destroy }
        it { is_expected.to be_truthy }
    end
  end

end

错误是:

代码语言:javascript
复制
 Failure/Error: let(:currency) { create(:currency) }

 ActiveRecord::RecordInvalid:
   A validação falhou: Name não está disponível

即使我禁用了模型中的存在和唯一性验证,问题仍然存在

谁能帮我

EN

回答 1

Stack Overflow用户

发布于 2021-03-06 14:37:19

您是否正确地创建了迁移,以便在数据库级别包含货币上的name

因为我在本地创建了迁移,并且通过了测试。

请看下面的代码。这就是我在本地做的,并且在这里工作!

1.迁移

db/migrate/2021XXXXXXXXXX_create_currencies.rb

  • 文件

代码语言:javascript
复制
class CreateCurrencies < ActiveRecord::Migration
  def change
    create_table :currencies do |t|
      t.string :name
    end
  end
end

2.模型

  • app/models/currency.rb

代码语言:javascript
复制
class Currency < ActiveRecord::Base
  has_many :countries

  validates :name, presence: true, uniqueness: true # Can be oneliner ;)

  before_destroy :safe_to_delete

  def safe_to_delete
    countries.empty? # Much simpler, right? ;)
  end
end

3.工厂

  • spec/factories/currency.rb

代码语言:javascript
复制
FactoryGirl.define do
  factory :currency do
    sequence(:name) { |i| "Currency-#{i}" }
  end
end

4.测试

  • spec/models/currency_spec.rb

代码语言:javascript
复制
require 'rails_helper'

describe Currency, type: :model do
  let(:currency1) { create(:currency) }
  let(:currency2) { create(:currency) }
  let(:country) { create(:country) }

  describe 'associations' do
    subject { currency1 }
    it { should have_many(:countries) }
  end

  describe 'validations' do
    subject { currency1 }
    it { should validate_presence_of(:name) }
    it { should validate_uniqueness_of(:name) }
  end

  describe 'when the currency is being deleted' do
    context 'with countries associated' do
      before { country.update_column(:currency_id, currency1.id)  }
      subject { currency1.destroy }
      it { is_expected.to be_falsy }
    end

    context 'with no countries associated' do
      before { country.update_column(:currency_id, currency2.id)  }
      subject { currency1.destroy }
      it { is_expected.to be_truthy }
    end
  end
end

测试执行

最后,测试应该能在上面的设置下正常工作!

  • spec/models/currency_spec.rb

代码语言:javascript
复制
rspec spec/models/currency_spec.rb

D, [2021-03-06T03:31:03.446070 #4877] DEBUG -- : using default configuration
D, [2021-03-06T03:31:03.449482 #4877] DEBUG -- : Coverband: Starting background reporting
.....

Top 5 slowest examples (0.10688 seconds, 11.4% of total time):
  Currency when the currency is being deleted with countries associated should be falsy
    0.04095 seconds ./spec/models/currency_spec.rb:23
  Currency associations should have many countries
    0.03529 seconds ./spec/models/currency_spec.rb:10
  Currency when the currency is being deleted with no countries associated should be truthy
    0.01454 seconds ./spec/models/currency_spec.rb:29
  Currency validations should validate that :name cannot be empty/falsy
    0.00812 seconds ./spec/models/currency_spec.rb:15
  Currency validations should validate that :name is case-sensitively unique
    0.00797 seconds ./spec/models/currency_spec.rb:16

Finished in 0.93948 seconds (files took 8.04 seconds to load)
5 examples, 0 failures

所有测试均通过✅

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66501226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档