RSpec-使用cancan视图进行测试

时间:2018-07-16 01:18:48

标签: ruby-on-rails rspec cancan

我有一个像这样的简单测试:

require 'rails_helper'

RSpec.feature "Evaluating Questions" do

    before do
        puts "before"
        @john = FactoryBot.create(:admin_user)
        login_as(@john, :scope => :admin_user)
        FactoryBot.create(:question)
        QuestionStatus.create!(name: "Approved")
        visit questions_path
    end


    scenario "A user evaluates a question correctly" do
        puts "scenario"
        visit questions_path
        click_link "Evaluate"
        select('Approved', :from => 'revision_history_question_status_id')

        click_button "Create Revision history"
        expect(page).to have_content("Question was successfully reviewed")
    end


end

问题在于,仅当用户具有cancan的许可时,才会显示“评估”链接。但是我在工厂创建的该用户具有权限。但是,rspec仍然无法在页面上找到链接,因此我想它没有被渲染。任何想法如何解决?谢谢

编辑:

Rspec错误消息:     失败/错误:click_link“评估”

 Capybara::ElementNotFound:
   Unable to find visible link "Evaluate"

1 个答案:

答案 0 :(得分:0)

发现了问题所在:我应该使用:scope => :user。以前,它无法登录,因此才找不到链接