【发布时间】:2018-06-07 14:19:55
【问题描述】:
我的控制器中有以下代码:
private
def remaining_words
@remaining_words = Vocab.all.where.not(id: session[:vocab_already_asked])
@questions_remaining = @remaining_words.length - 4
@quiz_words = @remaining_words.shuffle.take(4)
这是我的测试:
feature 'Quiz functionality' do
scenario "gets 100% questions right in quiz" do
visit(root_path)
visit(start_quiz_path)
assigns(:questions_remaining).length.to_i.times do
orig_value = find('#orig', visible: false).value
choose(option: orig_value)
click_on('Submit')
expect(page).to have_content('You got it right!')
expect(page).not_to have_content('Sorry, wrong answer!')
end
expect(page).to have_content("Your score is 27/27")
save_and_open_page
end
end
我在运行测试时收到错误消息:
NoMethodError: undefined method `assigns' for #<RSpec::ExampleGroups::QuizFunctionality:0x007f8f2de3f2b0>
# ./spec/features/quizzes_spec.rb:9:in `block (2 levels) in <top (required)>'
我也尝试过使用 controller.instance_variable_get(:remaining_words) 并收到此错误消息
NameError:
undefined local variable or method `controller' for #<RSpec::ExampleGroups::QuizFunctionality:0x007fc4b99251a0>
我在设置测试时是否遗漏了什么?我应该使用 describe 而不是 feature 来启用 assign 方法吗?
【问题讨论】:
标签: ruby-on-rails rspec capybara