[英]Ruby on Rails - Adding user in test case
I am really new to the unit testing and i am trying to wrap my head around it. 我对单元测试真的很陌生,我正在努力把它包裹住。 So the situation I have an article form where user can enter
title
and description
and on clicking Create Article
article should get created but only a logged in user can perform this operation so I need to test this. 因此,我有一个文章表单,用户可以在其中输入
title
和description
,然后单击“ Create Article
”就可以创建该文章,但是只有登录的用户才能执行此操作,因此我需要对此进行测试。
Since I am new to this so this is what I am thinking, 由于我是新手,所以这就是我的想法,
here is my try 这是我的尝试
require 'rails_helper'
RSpec.feature 'adding article' do
scenario 'allow user to add an article' do
@user = User.create(:email => "saadia1@clickteck.com", :password => 'password', :username => 'saadia1')
session[:user_id] = @user.id
visit new_article_path
# @article = Article.user.to eql(@user = User.find_by(id: 6))
fill_in "Title", with: "My Title"
fill_in "Description", with: "My description"
click_on("Create Article")
expect(page).to have_content("My Title")
expect(page).to have_content("My description")
end
end
When i run the command rspec spec/features/add_article_spec.rb
当我运行命令
rspec spec/features/add_article_spec.rb
I see 我懂了
Failures:
失败:
1) adding article allow user to add an article Failure/Error: session[:user_id] = @user.id
1)添加文章允许用户添加文章失败/错误:session [:user_id] = @ user.id
NameError: undefined local variable or method `session' for #<RSpec::ExampleGroups::AddingArticle:0x007f89285e32e8> # ./spec/features/add_article_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.0197 seconds (files took 1.35 seconds to load) 1 example, 1 failure
在0.0197秒内完成(文件花费1.35秒来加载)1例,1例失败
Failed examples:
失败的例子:
rspec ./spec/features/add_article_spec.rb:4 # adding article allow user to add an article
rspec ./spec/features/add_article_spec.rb:4#添加文章允许用户添加文章
So my question is how do i add an article as a logged in user? 所以我的问题是我如何以登录用户身份添加文章? I will really appreciate any help on this.
我将非常感谢对此的任何帮助。
are you using devise for authentication if yes devise provides some helpers for test also include this line in your rail_helper.rb 您是否正在使用devise进行身份验证(如果是),那么devise提供了一些测试助手,因此在rail_helper.rb中也包含此行
config.include Devise::Test::ControllerHelpers, :type => :controller
this will help you to use sign_in
helper method of devise, and you wil not need to use session as you are doing currently, Please refer to link for more information config.include Devise::Test::ControllerHelpers, :type => :controller
这将帮助您使用sign_in
helper方法,并且您将不需要像当前一样使用会话,请参考链接以获取更多信息
This is how I ended up creating a test case, since this is my one of the first test cases ever I am not sure how this can be improved so feel free to review it 这就是我最终创建测试用例的方式,因为这是我有史以来的第一个测试用例之一,所以我不确定如何改进它,因此可以随时对其进行回顾。
require 'rails_helper'
RSpec.feature 'adding article' do
scenario 'allow user to add an article' do
user = FactoryGirl.create(:user)
visit login_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Log in'
expect(page).to have_content('You have successfully logged in')
visit new_article_path
fill_in "Title", with: "My Title"
fill_in "Description", with: "My description"
click_on("Create Article")
expect(page).to have_content("My Title")
expect(page).to have_content("My description")
end
end
This is my factory 这是我的工厂
# spec/factories/users
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "user#{n}" }
password 'password'
sequence :email do |n|n
"user_#{n}@example.com"
end
end
end
Finished in 0.4176 seconds (files took 2.54 seconds to load) 1 example, 0 failures
只需0.4176秒即可完成(文件加载时间为2.54秒)1例,失败0例
I do still want to know if it is possible at all to login the user in another scenario or all this has to be a part of one scenario. 我仍然想知道是否完全有可能在另一种情况下登录用户,或者所有这些都必须是一种情况的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.