0
0
Ruby on Railsframework~20 mins

Controller tests in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Controller Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
AThe test fails with a routing error because the route is missing.
BThe test fails because the article is not saved to the database.
CThe test passes because the show action returns HTTP status 200.
DThe test raises a syntax error due to missing end keywords.
Attempts:
2 left
💡 Hint
Check if the article is created and the show action is called correctly.
state_output
intermediate
2: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
A'Error creating post.'
B'Post was successfully created.'
Cnil
DRaises a NoMethodError
Attempts:
2 left
💡 Hint
The post is created with valid parameters, so the success branch runs.
🔧 Debug
advanced
2: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
AThe test raises ActiveRecord::RecordNotFound because User.find(user.id) fails after deletion.
BThe test raises a NoMethodError because destroy is undefined.
CThe test fails because the user is not created before the test.
DThe test raises a routing error due to missing route.
Attempts:
2 left
💡 Hint
Check what happens when you try to find a deleted record.
📝 Syntax
advanced
2: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
Apatch :update, params: { id: comment.id, comment: { body: 'New' } }
Bpatch update, params: { id: comment.id, comment: { body: 'New' } }
Cpatch :update, id: comment.id, comment: { body: 'New' }
Dpatch :update, { id: comment.id, comment: { body: 'New' } }
Attempts:
2 left
💡 Hint
The params hash must be passed with the key :params.
🧠 Conceptual
expert
2: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.
ATo test the database schema and ensure migrations run without errors.
BTo replace integration tests by testing all user flows in one place.
CTo check the styling and layout of views rendered by the controller.
DTo verify that controller actions respond correctly to HTTP requests and manipulate data as expected.
Attempts:
2 left
💡 Hint
Think about what controllers do in a Rails app and what tests should confirm.