根据gem配置有条件地运行Rspec测试块

时间:2015-05-07 10:50:13

标签: ruby-on-rails ruby rspec config

我正在尝试添加一个rspec测试,该测试依赖于gem用户设置的配置。所以我想用一定的配置运行测试。

这是配置:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

一个例子是:

  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