首页 > 解决方案 > Ruby on Rails, jwt auth, capybara cucumber testing

问题描述

I have Ruby on Rails backend and one view where ReactJS frontend is mounted. Authentication by devise + jwt. I use cucumber capybara for testing frontend. How can I sign in in tests? How can I put jwt in headers in steps of test?

Feature

...
  Background:
   Given I am loged in user   
...

Steps

Given(/^I am loged in user$/) do
  how can I put jwt in headers here?
end

标签: ruby-on-railsdevisecucumbercapybara

解决方案


您可以创建一个帮助程序来填写您的登录表单,如下所示:

# spec/support/features_helpers.rb

def login_user(user=nil)
  visit '/#/login'

  within '#login_form' do
    fill_in 'email', with: user.email
    fill_in 'password', with: 'foobarfoo'
    page.find('button').click
  end

  expect(page).to have_no_css '#login_form'
end

然后在您的测试中使用:

let(:user) { create(:user) }

before do
  login_user user
end

it '...' do
  ...
end

推荐阅读