Challenge - 5 Problems
Rails Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why does Rails encourage testing by default?
Rails comes with built-in testing tools and generates test files automatically. Why is this approach important?
Attempts:
2 left
💡 Hint
Think about how testing affects catching errors before users see them.
✗ Incorrect
Rails encourages testing to help developers find and fix bugs early, making the app more reliable and easier to maintain.
❓ component_behavior
intermediate2:00remaining
What happens when a failing test is run in Rails?
Consider a Rails model test that expects a validation but the validation is missing. What will the test output show?
Ruby on Rails
class UserTest < ActiveSupport::TestCase test "should not save user without email" do user = User.new assert_not user.save end end
Attempts:
2 left
💡 Hint
What does a failing test usually do in any testing framework?
✗ Incorrect
When a test fails, Rails shows a clear message so developers know what went wrong.
❓ state_output
advanced2:00remaining
What is the output of this Rails controller test?
Given this test code, what will be the output status code when the test runs?
Ruby on Rails
class ArticlesControllerTest < ActionDispatch::IntegrationTest test "should get index" do get articles_url assert_response :success end end
Attempts:
2 left
💡 Hint
What does :success mean in HTTP status codes?
✗ Incorrect
The :success symbol corresponds to HTTP status code 200, meaning the request was successful.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this Rails test snippet
Which option contains a syntax error that will cause the test suite to fail loading?
Ruby on Rails
test "validates presence of name" do
user = User.new(name: nil)
assert_not user.valid?
endAttempts:
2 left
💡 Hint
Look carefully at how the test method is called and its string argument.
✗ Incorrect
Option B is missing quotes around the test name, causing a syntax error.
🔧 Debug
expert3:00remaining
Why does this Rails test raise a NoMethodError?
This test tries to check a model method but raises NoMethodError. What is the likely cause?
Ruby on Rails
class ProductTest < ActiveSupport::TestCase test "calculates discounted price" do product = Product.new(price: 100) assert_equal 90, product.discounted_price end end
Attempts:
2 left
💡 Hint
NoMethodError means calling a method that does not exist on the object.
✗ Incorrect
If the method discounted_price is not defined in the Product model, calling it raises NoMethodError.