I'm learning Ruby. I'm now trying to test one of my controller.
My test file is myapp/test/testing.rb while my controller is located at myapp/app/controllers/test_controller.rb.
The content of testing.rb is
data = testController.mymethod()
puts(data)
But when doing
ruby myapp/test/testing.rb
in the terminal, I get a warning :
Traceback (most recent call last):myapp/test/testing.rb:6:in `': uninitialized constant testController (NameError)
Could someone explain me what i'm doing is wrong and how I should do this ?
Thanks !
The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response.
Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework. Using the older approach of mocking out the whole framework with ActionDispatch::ControllerTest is no longer recommended for new applications.
This is your basic "Hello World" example of controller testing:
require 'test_helper'
class BlogFlowTest < ActionDispatch::IntegrationTest
test "can see the welcome page" do
get "/"
assert_select "h1", "Welcome#index"
end
end
Controllers can also be tested at a higher level through system tests which emulatate the user interacting with the application.
You can also use RSpec which is different test framework with a large following. In RSpec you write request specs which are just a thin wrapper on top of ActionDispatch::IntegrationTest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With