rspec - Ruby on Rails - Adding user in test case -
i new unit testing , trying wrap head around it. situation have article form user can enter title
, description
, on clicking create article
article should created logged in user can perform operation need test this.
since new thinking,
- i create user first
- save session thats how system checks if user logged in (but thinking not browser logic might not work), how submit form logged in user?
here try
require 'rails_helper' rspec.feature 'adding article' scenario 'allow user add article' @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 run command rspec spec/features/add_article_spec.rb
i see
failures:
1) adding article allow user add article failure/error: session[:user_id] = @user.id
nameerror: undefined local variable or method `session' #<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 load) 1 example, 1 failure
failed examples:
rspec ./spec/features/add_article_spec.rb:4 # adding article allow user add article
so question how add article logged in user? appreciate on this.
are using devise authentication if yes devise provides helpers test include line in rail_helper.rb
config.include devise::test::controllerhelpers, :type => :controller
use sign_in
helper method of devise, , wil not need use session doing currently, please refer link more information
Comments
Post a Comment