I was working on my rails application's tests and noticed some of my tests were failing after I added a login feature, since the views use the current user_id from the session variable, which was undefined during testing.
I tried to remedy this by creating a post request to create a user (a user can be a professor or a student for my app) and then to login with that user inside the test:
courses_controller_test.rb
setup do
@course = courses(:one)
@user = professors(:admin)
end
test "should get new" do
professor_http_code = post professors_path, params: {professor: {firstname:@user.firstname,
lastname: @user.lastname,
email: @user.email,
password: "123456",
password_confirmation: "123456"}}
puts "Professor post http code: " + professor_http_code.to_s
login_http_code = post login_path, params: {email: @user.email,
password: "123456",
type: {field: "professor"}}
puts "Login post http code: " + login_http_code.to_s
get new_course_url
assert_response :success
end
The test fails with the same problem (no current user when rendering the view) and produces the following output in the console:
Console output
Running via Spring preloader in process 22449
Run options: --backtrace --seed 26071
# Running:
.Professor create params: <ActionController::Parameters {"firstname"=>"foo", "lastname"=>"bar", "email"=>"foobar@gmail.com", "password"=>"123456", "password_confirmation"=>"123456"} permitted: false>
Professor not saved to db
..Professor post http code: 200
user login params: #<Professor id: 135138680, firstname: "foo", lastname: "bar", email: "foobar@gmail.com", created_at: "2020-03-31 02:12:50", updated_at: "2020-03-31 02:12:50", password_digest: nil>
Login post http code: 500
F
Failure:
CoursesControllerTest#test_should_get_new [/home/sruditsky/Homework/Capstone/team-formation-app/test/controllers/courses_controller_test.rb:25]:
Expected response to be a <2XX: success>, but was a <500: Internal Server Error>
And here are my session and professor controller functions which are handling the requests:
Professors Controller
class ProfessorsController < ApplicationController
...
def create
@professor = Professor.new(professor_params)
puts "Professor create params: " + params[:professor].inspect
respond_to do |format|
if @professor.save
puts "password_d: " + @professor.password_digest
log_in(@professor, "professor")
format.html { redirect_to @professor, notice: 'Professor was successfully created.' }
format.json { render :show, status: :created, location: @professor }
else
puts "Professor not saved to db"
format.html { render :new }
format.json { render json: @professor.errors, status: :unprocessable_entity }
end
end
end
...
Sessions Controller
class SessionsController < ApplicationController
...
def create
user = nil
type = params[:type][:field]
if type == "student"
user = Student.find_by_email(params[:email])
elsif type == "professor"
user = Professor.find_by_email(params[:email])
end
puts "user login params: " + user.inspect
if user && user.authenticate(params[:password])
puts "logging in"
log_in(user, type)
redirect_to root_url, notice: "Logged in!"
else
puts "invalid password"
flash.now[:alert] = "Email or password is invalid"
render "new"
end
end
...
The console output shows that the professor is not being saved to the database, but creating a professor account on the application works fine, and also when I type the following into the rails console in the test env it works fine:
app.post "/professors", params: {professor: {firstname: "foo", lastname: "bar", email: "foobar@gmail.com", password: "123456", password_confirmation: "123456"}}
I have tried adding a random authenticity_token to the params, hardcoding all the strings in the params instead of using the @user object, and dropping and recreating, migrating, loading, and preparing my test database and have had no luck.
Let me know if you need to see something else in my application to solve the problem, and any help would be super appreciated!