[英]Rails Cucumber How to check if user is logged in before going to editing page
I have developed Rails application where user needs to be logged in to edit character he created earlier. 我已经开发了Rails应用程序,需要用户登录才能编辑他之前创建的字符。 Now I am trying to test my edit character function and I have created edit character scenario.
现在,我正在尝试测试编辑字符功能,并创建了编辑字符场景。 Here is my files:
这是我的文件:
features/character.feature 功能/特性
Feature: Character
@javascript
Scenario: Visiting characters listing page
Given I am a new, authenticated user
When I go to characters index page
Then I should see characters listing page
characters_steps.rb character_steps.rb
Given /^I am a new, authenticated user$/ do
email = 'ademimisel@gmail.com'
password = 'pass1234'
visit '#/login'
fill_in 'email', :with => email
fill_in 'password', :with => password
click_button "Sign in"
end
When(/^I go to characters index page$/) do
visit '#/'
expect(page).to have_content("Characters")
end
Then(/^I should see characters listing page$/) do
pending # Write code here that turns the phrase above into concrete actions
end
When I run, first step pass successfully but second not, because cucumber not register that sign up was successfully done and page are not redirected to edit page 当我运行时,第一步成功通过,但第二步没有通过,因为黄瓜未注册成功完成了注册,并且页面未重定向到编辑页面
Assuming that you have created a user in the database matching the username and password, all you need is a final line in your "I am a new, authenticated user" step that checks for something that is shown on the page after logging in. 假设您已经在数据库中创建了一个与用户名和密码匹配的用户,那么您需要做的是“我是新的经过身份验证的用户”步骤中的最后一行,该行将检查登录后页面上显示的内容。
Given /^I am a new, authenticated user$/ do
email = 'ademimisel@gmail.com'
password = 'pass1234'
visit '#/login'
fill_in 'email', :with => email
fill_in 'password', :with => password
click_button "Sign in"
expect(page).to have_text("You are now logged in") # or whatever message is displayed to indicate successfull login - if using RSpec
# page.assert_text("You are now logged in") # if not using RSpec
end
If a user needs to be logged in before they can do something then you need to have the ability to 'register' as a user 'before' you can implement this scenario. 如果需要先登录用户,然后才能执行某项操作,则您需要能够在“先于”之前“注册”为用户,然后才能实施此方案。 Assuming you have done this you should be able to write something like
假设您已完成此操作,则应该可以编写如下内容
Given I am registered
And I am logged in
When I view characters
Then I should see a list of characters
You can implement the steps as follows 您可以执行以下步骤
Given 'I am registered' do
@i = create_registered_user
end
Given 'I am logged in' do
login as: @i
end
and implement these using a step helper module 并使用步骤帮助程序模块实施这些
module RegistrationStepHelper
def login(as:)
...
def create_registered_user
...
end
World RegistrationStepHelper
You should extract the code you've previously written to do these tasks and use that in these methods. 您应该提取先前编写的代码来完成这些任务,并在这些方法中使用它们。
Now if you haven't got this sort of capability from the code you have already written you need to create it and use it to ensure that there is a registered user in the database before you login to view your characters. 现在,如果您没有从已编写的代码中获得这种功能,则需要创建它并使用它来确保在登录查看字符之前数据库中已有注册用户。
From your question and the code you've posted there is no indication that such a user is in the database. 从您的问题和您发布的代码来看,没有迹象表明该用户在数据库中。 Each scenario starts with an empty database and your scenario is not registering a user.
每个方案都以一个空的数据库开头,并且您的方案没有注册用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.