Skip to content

Mock Model

Batu Tasvaluan edited this page May 13, 2020 · 1 revision

Structure

  • MockSource: Put all the mock scenario here.
  • MockScenario: Declare you scenario, including the basic info and target api endpoints.
    • A scenario might have several target api endpoints.
  • MockApi: Declare the mock api endpoint and the mock response model.

MockApi

4 elements

  1. The whole path
// if the api url is: https://test.com/the/path/of/member?id=1234
val urlPaths = listof("the", "path", "of", "member")
  1. html method
val htmlMethod = "GET" // Right now this library just support GET method
  1. parameters
// if the api url is: https://test.com/the/path/of/member?id=1234
val urlParams = mutableMapOf("id" to "1234")
  1. response object
// You can put DAO or json string
val responseObject = private fun getMembers(): String =
        "{\"age\":12,\"id\":132,\"name\":\"Belly World\"}"
        
val responseObject = Member(
    name = "Belly World",
    id = 132,
    age = 12
)

final object

val urlPaths = listof("the", "path", "of", "member")
val responseObject = Member(
    name = "Belly World",
    id = 132,
    age = 12
)
val mockApi = MockApi(urlParams).apply {
    htmlMethod = "GET"
    responseObject = responseObject
    urlParams = mutableMapOf("id" to "1234")
}

MockScenario

3 elements

  1. Basic info

    • It would show on Bubble Interface.
  2. The list of MockApi:

    • It might have muliple MockApi in one MockScenario.
    • Example: The display members' address info, we might call first api to get member id, then call second api to get the member address info.
  3. Selection State

    • Indicate the default selection state of this scenario.

Demo

vak mockApiList = listof(mockApi)
val mockScenario1 = MockScenario(
    page = "Main Page", 
    name = "Member List - same data with address"
).apply {
    selected = false
    mockApiList = mockApiList
}
        

DSL for MockScenario and MockApi

Another way to declare MockScenario and MockApi

val mockScenario = 
    scenario(page = "Main Page", name = "Member List - several") {
        select {
            true
        }
        add {
            api("the", "path", "of", "member") {
                params {
                    param("id", 1234)
                }
                response {
                    Member(
                        name = "Belly World",
                        id = 132,
                        age = 12
                    )
                }
            }
        }
        add {
            api("another", "path") {
                response {
                    AnotherObject()
                }
            }
        }
    }

MockSource

class MyMockSource : MockSource {
    val mockScenario = ... // just like code section above

    override fun create(): List<MockScenario> =
        mutableListOf<MockScenario>().apply {
            add(mockScenario)
        }
}

You can check the whole setting in sample code.

Some other detail

  • You have to make sure the whole api path and parameter are all exactly right.
  • You can Ignore the value of parameter by using ANY_PARAM_VALUE
// regular way
urlParams = mutableMapOf("id" to ANY_PARAM_VALUE)

/// DSL
params {
    param("id", ANY_PARAM_VALUE)
}
  • You can Ignore the value of api apth by using ANY_PATH (However, the size of path must be correct)
// regular way
val urlPaths = listof("the", "path", ANY_PATH, ANY_PATH)

// DSL
api("the", "path", ANY_PATH, ANY_PATH) {
    ...
}
Clone this wiki locally