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

Learning Notes-- A summary of the stages of test learning (Rspec Learning 1)

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Recently, the whole project team has been testing and learning, and has come into contact with several unfamiliar and advanced concepts to me. Relying on the information collected on the Internet, I have only scratched the surface, and so far, there are still some gains. So it is summarized in this place (part of the content comes from the Internet).

one。 What is behavior-driven development (Behavior-Driven Development) (BDD)

In software engineering, BDD is a software development process and method based on test-driven development (test-driven development TDD). As a design method, BDD can effectively improve the design and point out the way forward for the team in the process of system evolution.

The specific tool used in RubyOnRails is called cucumber, and this part can also be completely replaced by customer requirements, which is an important cornerstone of agile development. however, this article is not mainly about this, and we have not come into contact with any cucumber materials so far.

two。 What is test-driven development, English full name Test-Driven Development, (referred to as TDD)

Test-driven development, the English full name Test-Driven Development, referred to as TDD, is a new development method which is different from the traditional software development process. It requires that before writing the code of a function, write the test code, and then only write the function code that makes the test pass, through the test to promote the whole development. This helps to write concise, available and high-quality code and speed up the development process.

The specific tool used in RubyOnRails is called Rspec, which is also the main content of this article.

three。 General development process (project cycle)

In a project based on the BDD development model, take the Rails project development as an example, the steps are as follows:

1. After setting up a project, first decompose the story. To decompose the story of the system, we are required to do the basic requirements analysis and the outline design of the system.

2. Focus on a user story and define it in detail, which is the stage when we write RSpec test cases. The behavior of the system is defined in detail by analyzing user stories and user scenarios, and the definition is written in our test cases. In this stage, it includes the requirements analysis and detailed design phase of our traditional software engineering cycle, the requirements analysis is reflected in the analysis of the behavior of the system, and the detailed design is reflected in the need to define a large number of interfaces for the system implementation when writing use cases, which will be discussed in detail below.

3. For the user story, implement the View layer:

3.1.Running the RSpec test case failed

3.2.Encoding to realize the view

Re-RSpec to run the test case and pass the

3.4, ReFactor the code

4. For the user story, implement the controller layer and repeat the process of 3.13.4.

5. For the user story, implement the model layer and repeat the process of 3.13.4.

6. Reconstruct three levels of code to complete the user story.

7. Repeat the process of 3-6 to complete other user stories of the system.

8. System integration, integration testing, confirmation testing, beta testing, etc.

9. System delivery and post-maintenance.

The process is illustrated as follows:

It is emphasized here that the development should be done in the order of view, controller to model. BDD encourages the development of programs from the outside to the inside, first realizing the view layer that directly interacts with users, then the core business layer controller, and finally the model layer that interacts with the database. BDD is not only a technology used in our development programs, but also can reflect the user interface of the system as soon as possible, reflecting the commercial value of the system. The behavior of users when interacting with the system is the core business of the system. Developing the user interface first will help us to focus on the implementation of the core business without wasting energy on other aspects.

four。 Configuration and Rspec practice

1. Installation and configuration

If you have an existing project, you can configure it directly in gemsfile as follows (due to the lower version of jruby used by the author) group: development,: test do

Gem "database_cleaner"

Gem "rspec-rails"

Gem "cucumber-rails"

Gem "webrat"

Gem "rspec-mocks"

Gem "spork",'~ > 1.0rc'

Gem "factory_girl",'~ > 2.6.4'

Gem "factory_girl_rails",'~ > 1.7.0'

End

The main configuration plug-ins are

Rspec-rails

Cucumber-rails

Factory_girl_rails (simulates large amounts of test data)

After being configured here, the relevant plug-ins can be installed automatically by bundle update directly.

Of course, it can also be installed in gem install XXXXX mode.

The resulting directory structure is shown in the figure

Tips: if you use the IDE of rubymine, you can use the bundle operation in its tools tool options, or quick install gem to install and maintain plug-ins

2. Partial testing practice of control (mainly using the piling technology of stub)

The code is as follows

Before (: each) do

@ metadatum = mock_model (Metadatum)

@ metadatum.stub (: subscribe_to_the_number_of) .and_return ("1")

@ metadatum.stub (: save) .and_return (true)

End

It "Get index should not nil" do

Get "index"

Response.should_not be_nil

Assigns [: metadatas] .should _ not be_nil

End

First of all, we can see a format that is mainly used in general Rspec testing

Describe "Test description" do

Befor (: each) do

What needs to be executed for each test column

End

It "Test case description" do

Specific test code

End

End

The return results of response in the controller may be different, and several types of data tests are mainly used in the author's project.

Redirect_to (redirect)

In specific tests, we can write like this.

Response.should redirect_to (: action = >: index)

For flash data, we need to verify its content, and we can write it like this.

Flash [: notice] .should eq ('your expected results')

You can use render_template to accept the data content of render as follows

Response.should render_template (: key used in action = > specific value)

When we call action in action, in order to ensure the decoupling of the system, we need to intercept the calling action and return values, so we can use mock.

For example, the one used above

@ metadatum = mock_model (Metadatum)

Metadatum.stub (: new) .and_return (@ metadatum)

@ metadatum.stub (: subscribe_to_the_number_of) .and_return ("1")

@ metadatum.stub (: save) .and_return (true)

These are the methods we use to simulate the Metadatum model.

Testing of 3.model (mainly using factory_girl to build data technology)

For the test of the model, we mainly use factory_girl to do a lot of data simulation, the specific code is as follows

In the..\ spec\ factories directory, we mainly build test data.

FactoryGirl.define do

Factory: metadatum do

Abstract "Administrative orders"

Metadata_identifier "administrative"

Metadatadate_update Time.now

Resource_type "About administrative orders"

Upload_state Date.today

Online_source "orders"

Publication_data Date.today

Resource_degree ""

Resource_id 1

Resource_title 1

Verify_enable 1

End

End

Call this data method in our specific model test as follows

..\ spec\ models\ metadatum_rspec.rb

Before (: each) do

@ metadata = Factory.create (: metadatum)

End

It "This' verify_enable' should be 0" do

@ metadata.set_resource_type (0)

Expect (@ metadata.resource_type) .to eq (0)

End

I use the expect assertion here. Once a failure occurs, it will be reflected in the test. Of course, we can also use the should method to test, so the last sentence should be written like @ metadata.resource_type.should = = 0.

five。 Summary

After a period of study, I feel about the feeling of testing, but I still have a lot to learn. I just learned something about factory tonight, and there are a lot of things I haven't done yet. I haven't learned view and gem testing yet. Of course, the above things are not necessarily comprehensive and correct. I will continue to maintain the test learning part in the future.

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