0
0
Ruby on Railsframework~20 mins

Why testing is integral to Rails - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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?
AIt only helps when using third-party gems, not for your own code.
BIt makes the app run faster in production by skipping tests.
CIt helps developers catch bugs early and maintain code quality as the app grows.
DIt forces developers to write more code without any real benefit.
Attempts:
2 left
💡 Hint
Think about how testing affects catching errors before users see them.
component_behavior
intermediate
2: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
AThe test fails and shows an error message explaining the failure.
BThe test passes silently without any output.
CThe Rails server crashes immediately.
DThe test is skipped automatically.
Attempts:
2 left
💡 Hint
What does a failing test usually do in any testing framework?
state_output
advanced
2: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
A302
B404
C500
D200
Attempts:
2 left
💡 Hint
What does :success mean in HTTP status codes?
📝 Syntax
advanced
2: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?
end
A
test "validates presence of name" do
  user = User.new(name: nil)
  assert_not user.valid?
end
B
test validates presence of name do
  user = User.new(name: nil)
  assert_not user.valid?
end
C
test "validates presence of name" do
  user = User.new(name: nil)
  assert_not user.valid?
D
test "validates presence of name" do
  user = User.new(name: nil)
  assert user.valid?
end
Attempts:
2 left
💡 Hint
Look carefully at how the test method is called and its string argument.
🔧 Debug
expert
3: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
AThe Product model does not have a method named discounted_price defined.
BThe test syntax is incorrect and causes the error.
CThe price attribute is missing from the Product model.
DThe assert_equal arguments are reversed causing the error.
Attempts:
2 left
💡 Hint
NoMethodError means calling a method that does not exist on the object.