ruby on rails - undefined method for nil:NilClass in RSpec -
I am using RSpec, and I get this error when I run tests:
Failure / Error: {edit_job_path (job)} Before Nomthious error: Undefined method `Jobs for zero ': NilClass # ./app/controllers/jobs_controller.rb: 63: in` correct_user' # ./spec/ Requests / authentication_pages_spec.rb: 35: `block (level 6) & lt; Top (required) & gt; The actual behavior of the page (when the rail server is doing) seems OK. Why the exam is not working? I'm still starting to ruby / rail, so any help would be appreciated.
Some code for help:
authentication_pages_spec.rb Required "Authentication" description Do the "sign in" description [...] describe the description of "authorization" for "non-signed users" before "describing" {edit_job_path (job)} before {fancygear.reditit ([Jobs, User: Users]}} {not_contot_content ('Editing Job'}} "After signing in" description [...] and [...] and end [...] and end < / Code> jobs_co Ntroller .rb Class Jobs Controller & lt; ApplicationController before_action: sign_in_user, only: [new: create, update] first_action: correct_user, only: [edit: update, delete] first_action: set_job, only: [: show, edit:, update: deleted ] DEF index @ jobs = job All F.D.F. shows F. Df New @ Job = Job Newfdef edits can make AD def @job = current_user Job Build (job_perms) answer_to dot | Format | If @job.save format.html {redirect_to @job, notice: 'Job was created successfully.' } Format.json {Render Action: 'Show', Status :: Created, Location: @ Job} and Format: {Ender: 'New'} format.json {render json: @job. Errors, status: unprocessable_entity} End end end DEF update respond_to do | Format | If @job.update (job_params) format.html {redirect_to @job, notice: 'The job was successfully updated.' } Format.json {head: no_content} else format.html {render action: 'edit'} format.json {render json: @ job.errors, status :: unprocessable_entity} end end end df delete @job.destroy response_to do | Format | [Edit] : Internship, postdate, filldate, location,: link,: description) end def correct_user @job = current_user.jobs.find_by (id: params [: id]) redirect_to root_url, notice: 'You can only edit your job 'If @job.nil? Factory Factory: user do sequence (: name) {| N | End end factories RB "Johnny # {n}"} sequence (: email) {| N | "Johnny_#{n}@example.com"} Password "Sampling" password_confirmation "Sample" End Factory: Sequence of Job (title) {| N | "Example title # {n}"} End of user end sessions_helper.rb Module session bhelar [. ..] Def current_user = (user) @current_user = user end def current_user remember_token = User.encrypt (Cookies [: remember_token]) @current_user || = User.find_by (remember_token: remember_token) end [...] def sign_in? ! Current_user.nil? Unless signed_in has been done, Store_location redirect_to signin_url, notice: "Please sign in." End end [...] def store_location session [: return_to] = request.url if request.get? End [...] end
Your test is working well because they are The bug is open Your operation is expected to be set to current_user , and not so, so you get an error. You need the before_filter which redirects you when you press a page that requires authorization.
Comments
Post a Comment