运行 Rspec 根据 gem 配置有条件地测试块
Run Rspec test block conditionally based on gem 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
尝试使用 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
我最终使用的解决方案是在正确的 context/describe 块中阻塞之前设置设置。
一个例子是这样的:
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
我正在尝试添加一个 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
尝试使用 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
我最终使用的解决方案是在正确的 context/describe 块中阻塞之前设置设置。
一个例子是这样的:
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