Authentication Authentication Using Devise and RSpec Utility

I have an example of a Rails application I'm working on, so I can start getting to know BDD.

I am now using Devise to handle my user authentication.

I have a task model that requires the user to register in order to access new, create, edit and delete actions.

I have the following code:

Queries / tasks _spec.rb

describe "Tasks" do it "redirects to sign in for new task if no user logged in" do visit new_task_path response.should redirect_to(new_user_session_path) end end 

Now when I run this test, I expect it to fail because I did not add the before_filter logic to the TasksController. I run the test and it fails.

Now I add the authenticate_user! helper authenticate_user! provided by Devise.

tasks_controller.rb

 class TasksController < ApplicationController before_filter :authenticate_user!, only: [:new, :edit, :create, :destroy] ... end 

Testing is still in progress, saying the following:

  1) TasksController redirects to sign in for new task if no user logged in Failure/Error: response.should redirect_to(new_user_session_path) Expected response to be a <:redirect>, but was <200> 

Now, when calling a visit, since no users were set in the session, I expect the response to be redirected to / user / sign _in. However, I just return 200OK.

What am I missing here? How do I pass this test?

+4
source share
1 answer

Try the following:

 describe "redirects to sign in for new task if no user logged in" do before { post tasks_path } specify { response.should redirect_to(new_user_session_path) } end 
+2
source

Source: https://habr.com/ru/post/1444474/


All Articles