Challenge - 5 Problems
Rails Controller Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Rails controller test?
Given the following test for a Rails controller action, what will be the test result?
Ruby on Rails
require 'rails_helper' RSpec.describe ArticlesController, type: :controller do describe 'GET #show' do let(:article) { Article.create(title: 'Test', content: 'Content') } it 'returns a successful response' do get :show, params: { id: article.id } expect(response).to have_http_status(:success) end end end
Attempts:
2 left
💡 Hint
Check if the article is created and the show action is called correctly.
✗ Incorrect
The test creates an article and calls the show action with its id. The response should be successful (HTTP 200).
❓ state_output
intermediate2:00remaining
What is the value of flash[:notice] after this controller test runs?
Consider this controller test snippet. What will be the value of flash[:notice] after the POST create action?
Ruby on Rails
RSpec.describe PostsController, type: :controller do describe 'POST #create' do it 'sets a flash notice on success' do post :create, params: { post: { title: 'Hello', body: 'World' } } # What is flash[:notice] here? end end end # Controller code: # def create # @post = Post.new(post_params) # if @post.save # flash[:notice] = 'Post was successfully created.' # redirect_to @post # else # render :new # end # end
Attempts:
2 left
💡 Hint
The post is created with valid parameters, so the success branch runs.
✗ Incorrect
Since the post saves successfully, flash[:notice] is set to the success message.
🔧 Debug
advanced2:00remaining
Why does this controller test raise an error?
This test raises an error when run. What is the cause?
Ruby on Rails
RSpec.describe UsersController, type: :controller do describe 'DELETE #destroy' do it 'deletes the user' do user = User.create(name: 'Alice') delete :destroy, params: { id: user.id } expect(User.find_by(id: user.id)).to be_nil end end end # Controller code: # def destroy # User.find(params[:id]).destroy # redirect_to users_path # end
Attempts:
2 left
💡 Hint
Check what happens when you try to find a deleted record.
✗ Incorrect
After deletion, User.find(user.id) raises ActiveRecord::RecordNotFound because the record no longer exists.
📝 Syntax
advanced2:00remaining
Which option fixes the syntax error in this controller test?
Identify the correct syntax to test a PATCH update action in Rails controller tests.
Ruby on Rails
RSpec.describe CommentsController, type: :controller do describe 'PATCH #update' do let(:comment) { Comment.create(body: 'Old') } it 'updates the comment body' do patch :update, params: { id: comment.id, comment: { body: 'New' } } comment.reload expect(comment.body).to eq('New') end end end
Attempts:
2 left
💡 Hint
The params hash must be passed with the key :params.
✗ Incorrect
Option A uses the correct syntax with :update and params hash. Other options have syntax errors or missing keys.
🧠 Conceptual
expert2:00remaining
Which option best describes the purpose of controller tests in Rails?
Select the statement that most accurately explains why we write controller tests in Rails applications.
Attempts:
2 left
💡 Hint
Think about what controllers do in a Rails app and what tests should confirm.
✗ Incorrect
Controller tests check that actions handle requests properly, return correct responses, and change data as intended.