Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Rails Test "Nine" Integration Test integration test

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

opening remarks

Today we'll be familiar with rails integration test.

profile

Integration testing is mainly to test the interaction between multiple controllers, as well as to test the more important workflows in the application, and verify whether these workflows meet the expected assumptions.

Unlike unit tests and functional tests, which are added automatically. Integration tests are something we need to add manually, rails provides a command

rails generate integration_test

You can create an integration test in the test/integration folder with the command.

$ rails generate integration_test user_flowsexists test/integration/create test/integration/user_flows_test.rb

Let's start by looking at some of the help methods commonly used in integration testing.

https?

Returns true if the session is simulating an https request.

https!

Allows you to simulate https requests.

host!

Allows you to set the host name on the next request.

redirect?

Returns true if the last request was a jump.

follow_redirect!

followed by a jump response.

request_via_redirect(http_method, path, [parameters], [headers])

Sends an http request to the specified path, optional parameters, optional headers, followed by a jump.

post_via_redirect(path, [parameters], [headers])

Sends a post request to the specified path, optional parameters, optional headers, followed by a jump.

get_via_redirect(path, [parameters], [headers])

Sends a get request to the specified path, optional parameters, optional headers, followed by a jump.

put_via_redirect(path, [parameters], [headers])

Send a put request to the specified path, optional parameters, optional headers, followed by a jump.

delete_via_redirect(path, [parameters], [headers])

Sends a delete request to the specified path, optional parameters, optional headers, followed by a jump.

open_session

Start a new session.

example

Let's add some code to the created integration test file test/integration/user_flows_test.rb.

require 'test_helper'class UserFlowsTest

< ActionDispatch::IntegrationTestinclude FactoryGirl::Syntax::Methodsdef test_admin_login_and_browse_postsuser = FactoryGirl.create(:user_valid)get "/signin" assert_response(200)post_via_redirect("sessions", {:user=>

{:email=> user.email, :password => user.password}})assert_equal "/", pathassert_equal "sign in successfully", flash[:notice]get "admin/posts" assert_response(200)assert assigns(:posts)end end

In the above code, we first add a record to the users table. Then access signin and assert whether to return 200.

Then submit the user mailbox and password you just added to sessions. sessionscontroller is the controller responsible for verifying user information. Then assert whether the jump to the root directory, whether the correct flash prompt appears.

Finally visit admin/posts, assert whether to return 200, and return the posts variable.

The above tests involve multiple controllers, covering scheduling assignments from the database to the controller.

We can simulate multiple sessions simultaneously and extend those sessions with extend to create some powerful DSL(Domain-Specific Language) for testing.

Let's change the test above to look like this.

require 'test_helper'class UserFlowsTest

< ActionDispatch::IntegrationTestinclude FactoryGirl::Syntax::Methodsdef test_admin_login_and_browse_postsuser = FactoryGirl.create(:user_valid)guest = FactoryGirl.create(:user_valid_too)user_session = signin(user)guest_session = signin(guest)assert_equal("sign in successfully", user_session.flash[:notice])assert_equal("sign in successfully", guest_session.flash[:notice])user_session.browse_siteguest_session.browse_siteend privatemodule CustomDSLdef browse_siteget "admin/posts" assert_response(200)assert assigns(:posts)end end def signin(user)open_session do |sess|sess.extend(CustomDSL)sess.post_via_redirect("sessions", {:user =>

{:email => user.email, :password => user.password}})end end end

What is DSL (Domain Description Language)?

I understand it as business description language. Our application is generally oriented to an industry, or a domain, and the language of the business is the domain description language.

If you can describe the test process in the business language of the domain, then the test is closer to the business and has strong communication skills. This means that this test can be used to communicate with the business to see if it is the business process they want.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report