【问题标题】:Run Rspec test block conditionally based on gem config根据 gem config 有条件地运行 Rspec 测试块
【发布时间】:2015-05-07 10:50:13
【问题描述】:

我正在尝试添加编写一个依赖于 gem 用户设置为配置的 rspec 测试。所以我想用某种配置运行测试。

这是配置:

Tasuku.configure do |config|
  config.update_answers = false
end

这是当然只有在上面的配置设置为 false 时才有意义的测试:

  describe '#can_only_answer_each_question_once' do
    let!(:question)          { create :question_with_options }
    let!(:answer)           { create :question_answer, author: user, options: [question.options.first] }
    let!(:duplicate_answer) { build :question_answer, author: user, options: [question.options.first] }

    it 'prohibits an author from answering the same question more than once' do
      expect(duplicate_answer).not_to be_valid
    end

    it 'should have errors' do
      expect(duplicate_answer.errors_on(:base)).to eq [I18n.t('tasuku.taskables.questions.answers.already_answered')]
    end
  end

【问题讨论】:

    标签: ruby-on-rails ruby rspec config


    【解决方案1】:

    尝试使用RSpec 的过滤器。更多信息在这里:https://www.relishapp.com/rspec/rspec-core/v/2-8/docs/filtering/if-and-unless

    例如:

    describe '#can_only_answer_each_question_once', unless: answers_updated? do
    

    【讨论】:

    • 谢谢,这可能是一个不错的选择。但是我意识到需要为多个场景运行测试,但配置不同。我正在更新我的问题文本以反映这一点。
    【解决方案2】:

    我最终使用的解决方案是在它阻塞在正确的上下文/描述块之前设置设置。

    一个例子是这样的:

      describe '#can_only_vote_once_for_single_choice_questions' do
        before(:all) do
          ::Tasuku.configure do |config|
            config.update_answers = false
          end
        end
    
        let!(:question) { create :question_with_options, multiple: false }
        let!(:answer)   { build :question_answer, author: user, options: [question.options.first, question.options.second] }
    
        it 'prohibits an author from answering the same question more than once' do
          expect(answer).not_to be_valid
        end
    
        it 'should have errors' do
          expect(answer.errors_on(:base)).to eq [I18n.t('tasuku.taskables.questions.answers.can_only_vote_once')]
        end
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多