如果没有先执行

时间:2016-04-14 09:11:58

标签: ruby-on-rails rspec-rails

以下对我的代码不是很重要,但我只是想了解这种行为。如果我运行规范,则针对destroy操作的最后一个测试将失败,结果为true,而不是false。它失败,除非规范的随机顺序在规范之上设置销毁规范,然后它成功。

我的规范文件:

RSpec.describe ParticipationsController, type: :controller do

  before(:each) { create(:bree) }

  let(:bree) { Project.find_by_slug('bree') }
  let(:contact) { create(:armenia_contact) }
  let(:second_bree) { EcassoFile::File.find_by_reference('bree_02') }
  let(:third_bree) { EcassoFile::File.find_by_reference('bree_03') }
  let(:armenia) { ContactData::Contact.find_by_last_name('Armenia AG') }
  let(:sam) { ContactData::Contact.find_by_last_name('Gamgee') }

  context 'POST #create' do
    login_admin

    it 'should add a participant' do
      post :create, project_id: bree.slug, file_id: second_bree.reference, submit: t('defaults.save'),
        ecasso_file_participation: { part_type: 'client', part_desc_id: 2, contact_id: armenia.id }
      expect(assigns(:file).complete?).to be true
      expect(ContactData::Contact.find(second_bree.clients.first.id).last_name).to eq 'Armenia AG'
    end

    it 'should not add a participant without a contact' do
      post :create, project_id: bree.slug, file_id: second_bree.reference, submit: t('defaults.save'),
        ecasso_file_participation: { part_type: 'client', part_desc_id: 2 }
      expect(assigns(:file).complete?).to be false
      expect(second_bree.clients.first).to be nil
    end
  end

  context 'PUT #update' do
    login_admin

    it 'should update a participant contact' do
      put :update, project_id: bree.slug, file_id: second_bree.reference, id: second_bree.opponents.first.id,
        ecasso_file_participation: { part_type: 'opponent', part_desc_id: 1, contact_id: sam.id }
      expect(ContactData::Contact.find(second_bree.opponents.first.id).last_name).to eq 'Gamgee'
    end

    it 'should update a participant type' do
      put :update, project_id: bree.slug, file_id: second_bree.reference, id: second_bree.opponents.first.id,
        ecasso_file_participation: { part_type: 'client', part_desc_id: 2, contact_id: sam.id }
      expect(ContactData::Contact.find(second_bree.clients.first.id).last_name).to eq 'Gamgee'
    end
  end

  context 'DELETE #destroy' do
    login_admin

    it 'should set the active field to false' do
      expect(third_bree.has_debtor?).to be true
      delete :destroy, project_id: bree.slug, file_id: third_bree.reference, id: third_bree.opponents.first.id
      third_bree.participations.opponent.reload
      expect(third_bree.has_debtor?).to be false
    end
  end
end

以下是控制器摘录:

def destroy
    respond_to do |format|
      if @participation.update_attributes(active: false)
        format.html { redirect_to project_file_path(@project.slug, @file.reference),
          success: t('ecasso_files.delete_participation_success') }
      else
        format.html { redirect_to project_file_path(@project.slug, @file.reference),
          error: t('ecasso_files.delete_participation_error') }
      end
    end
  end
end

有人可以解释这种行为吗?

补充说明:销毁操作不应该真正消除数据库记录,而不是删除我将活动标志设置为“false'。

rails_helper.rb的摘录:

RSpec.configure do |config|
  config.include Capybara::DSL
  # Factory Girls settings
  config.include FactoryGirl::Syntax::Methods

  config.include Rails.application.routes.url_helpers
  config.include SessionsHelper, type: :controller
  config.extend ControllerMacros, type: :controller
  config.include LoginHelper
  config.include EcassoFilesHelper

  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!

  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation, reset_ids: true)
  end

  config.before(:each) do |example|
    DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

0 个答案:

没有答案