Ruby on Rails
Saturday, July 15, 2017
This is a continuation of
Setting up the relevant code
In config/routes.rb I have
In spec/controllers/sql_statements_controller_spec.rb I have
In app/controllers/sql_statements_controller.rb I hav
In app/policies/sql_statement_policy.rb
p_here2 is debugging code I find extremely useful
Analysis
Let's focus on app/controllers/sql_statements_controller.rb. The line to focus on is
With the line above in, I see this when I run rspec
If I comment out
I see this:
So, I think it is fairly clear Pundit is causing the 302.
Questions
I am a novice in both Pundit and Rspec
Question 1: Why is Pundit causing this "problem"?
Question 2: What is the preferred way to have Rspec go green on the test in spec/controllers/sql_statements_controller_spec.rb ?
-- Rspec. Is 302 a success? Why 302?
https://groups.google.com/forum/#!topic/rubyonrails-talk/Merk1uUyhyc
Setting up the relevant code
In config/routes.rb I have
Rails.application.routes.draw do
resources :sql_statements
resources :articles
devise_for :users
root 'static_pages#root'
resources :users
# root to: "articles#index"
# byebug
end
In spec/controllers/sql_statements_controller_spec.rb I have
describe "GET #new" do
it "returns a success response" do
get :new, params: {}, session: valid_session
puts; p_here2(response.status)
byebug
expect(response).to be_success
end
end
In app/controllers/sql_statements_controller.rb I hav
e
# GET /sql_statements/new
def new
@sql_statement = SqlStatement.new
authorize @sql_statement # I think this causes a 302
end
In app/policies/sql_statement_policy.rb
# See https://code.tutsplus.com/tutorials/authorization-with-pundit--cms-28202
#app/policies/sql_statement_policy.rb
class SqlStatementPolicy < ApplicationPolicy
def index?
true
end
def create?
user.present?
end
def update?
return true if user.present? && user == sql_statement.user
end
def destroy?
return true if user.present? && user == sql_statement.user
end
private
def sql_statement
record
end
end
p_here2 is debugging code I find extremely useful
def p_here2(*args)
# byebug
caller_text = caller[0]
puts caller_text
args.each_with_index do |value, index|
printf("%d: ", index)
pp value
end
Analysis
Let's focus on app/controllers/sql_statements_controller.rb. The line to focus on is
authorize @sql_statement # I think this causes a 302
With the line above in, I see this when I run rspec
/home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb:63:in `block (3 levels) in <top (required)>'
0: 302
[60, 69] in /home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb
60: describe "GET #new" do
61: it "returns a success response" do
62: get :new, params: {}, session: valid_session
63: puts; p_here2(response.status)
64: byebug
=> 65: expect(response).to be_success
66: end
67: end
68:
69: describe "GET #edit" do
(byebug)
If I comment out
#
authorize @sql_statement # I think this causes a 302
/home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb:63:in `block (3 levels) in <top (required)>'
0: 200
[60, 69] in /home/real-estate-data-mining/spec/controllers/sql_statements_controller_spec.rb
60: describe "GET #new" do
61: it "returns a success response" do
62: get :new, params: {}, session: valid_session
63: puts; p_here2(response.status)
64: byebug
=> 65: expect(response).to be_success
66: end
67: end
68:
69: describe "GET #edit" do
(byebug)
So, I think it is fairly clear Pundit is causing the 302.
Questions
I am a novice in both Pundit and Rspec
Question 1: Why is Pundit causing this "problem"?
Question 2: What is the preferred way to have Rspec go green on the test in spec/controllers/sql_statements_controller_spec.rb ?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f928dc0b-9f17-4408-a67f-a99076b4fdfd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment