2015-05-07 46 views
0

我正在尝试添加写入rspec测试,该测试取决于gem用户设置为配置的内容。所以我想用某种配置运行测试。基于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 

回答

0

我结束UT用在正确的背景下它会阻止前右侧设置设置解决方案/描述块。

一个例子是,是这样的:

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 
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 
+0

谢谢你,这也许是一个不错的选择。不过,我意识到需要运行测试多个scenarious但具有不同的配置。我正在更新我的quesiton文本以反映这一点。 –