класс ActionDispatch::IntegrationTest
Интеграционный тест охватывает несколько контроллеров и действий, связывая их все вместе, чтобы убедиться, что они работают вместе как ожидается. Он тестирует более полно, чем unit или функциональные тесты, используя весь стек, от диспетчера до базы данных.
В самом простом случае, вы просто расширяете IntegrationTest и пишете свои тесты, используя методы get/post:
require "test_helper"
class ExampleTest < ActionDispatch::IntegrationTest
fixtures :people
def test_login
# get the login page
get "/login"
assert_equal 200, status
# post the login and follow through to the home page
post "/login", username: people(:jamis).username,
password: people(:jamis).password
follow_redirect!
assert_equal 200, status
assert_equal "/home", path
end
end
Однако, вы также можете иметь несколько сеансов в каждом тесте и даже расширить эти сеансы утверждениями и методами, чтобы создать очень мощный DSL для тестирования, специфичный для вашего приложения. Вы даже можете ссылаться на любые именованные маршруты, которые вы определили.
require "test_helper"
class AdvancedTest < ActionDispatch::IntegrationTest
fixtures :people, :rooms
def test_login_and_speak
jamis, david = login(:jamis), login(:david)
room = rooms(:office)
jamis.enter(room)
jamis.speak(room, "anybody home?")
david.enter(room)
david.speak(room, "hello!")
end
private
module CustomAssertions
def enter(room)
# reference a named route, for maximum internal consistency!
get(room_url(id: room.id))
assert(...)
...
end
def speak(room, message)
xml_http_request "/say/#{room.id}", message: message
assert(...)
...
end
end
def login(who)
open_session do |sess|
sess.extend(CustomAssertions)
who = people(who)
sess.post "/login", username: who.username,
password: who.password
assert(...)
end
end
end Публичные методы класса
# File actionpack/lib/action_dispatch/testing/integration.rb, line 493 def self.app @@app || ActionDispatch.test_app end
# File actionpack/lib/action_dispatch/testing/integration.rb, line 497 def self.app=(app) @@app = app end
Публичные методы экземпляра
# File actionpack/lib/action_dispatch/testing/integration.rb, line 501 def app super || self.class.app end
Вызывает метод суперкласса
# File actionpack/lib/action_dispatch/testing/integration.rb, line 510 def document_root_element html_document.root end
# File actionpack/lib/action_dispatch/testing/integration.rb, line 505 def url_options reset! unless integration_session integration_session.url_options end
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.