What does “to stub” mean in programming?

后端 未结 10 942
盖世英雄少女心
盖世英雄少女心 2020-12-12 10:06

For example, what does it mean in this quote?

Integrating with an external API is almost a guarantee in any modern web app. To effectively test such i

相关标签:
10条回答
  • 2020-12-12 10:15

    Stub is a piece of code which converts the parameters during RPC (Remote procedure call).The parameters of RPC have to be converted because both client and server use different address space. Stub performs this conversion so that server perceive the RPC as a local function call.

    0 讨论(0)
  • 2020-12-12 10:17

    Layman's terms, it's dummy data (or fake data, test data...etc.) that you can use to test or develop your code against until you (or the other party) is ready to present/receive real data. It's a programmer's "Lorem Ipsum".

    Employee database not ready? Make up a simple one with Jane Doe, John Doe...etc. API not ready? Make up a fake one by creating a static .json file containing fake data.

    0 讨论(0)
  • 2020-12-12 10:20

    RPC Stubs

    • Basically, a client-side stub is a procedure that looks to the client as if it were a callable server procedure.
    • A server-side stub looks to the server as if it's a calling client.
    • The client program thinks it is calling the server; in fact, it's calling the client stub.
    • The server program thinks it's called by the client; in fact, it's called by the server stub.
    • The stubs send messages to each other to make the RPC happen.

    Source

    0 讨论(0)
  • 2020-12-12 10:22

    "Stubbing-out a function means you'll write only enough to show that the function was called, leaving the details for later when you have more time."

    From: SAMS Teach yourself C++, Jesse Liberty and Bradley Jones

    0 讨论(0)
  • 2020-12-12 10:25

    Stub is a function definition that has correct function name, the correct number of parameters and produces dummy result of the correct type.

    It helps to write the test and serves as a kind of scaffolding to make it possible to run the examples even before the function design is complete

    0 讨论(0)
  • 2020-12-12 10:27

    A stub is a controllable replacement for an Existing Dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly.

    External Dependency - Existing Dependency:
    It is an object in your system that your code under test interacts with and over which you have no control. (Common examples are filesystems, threads, memory, time, and so on.)

    Forexample in below code:

    public void Analyze(string filename)
        {
            if(filename.Length<8)
            {
                try
                {
                    errorService.LogError("long file entered named:" + filename);
                }
                catch (Exception e)
                {
                    mailService.SendEMail("admin@hotmail.com", "ErrorOnWebService", "someerror");
                }
            }
        }
    

    You want to test mailService.SendEMail() method, but to do that you need to simulate an Exception in your test method, so you just need to create a Fake Stub errorService object to simulate the result you want, then your test code will be able to test mailService.SendEMail() method. As you see you need to simulate a result which is from an another Dependency which is ErrorService class object (Existing Dependency object).

    0 讨论(0)
提交回复
热议问题