简体   繁体   English

具有PACT的XCUITest不返回模拟网络响应

[英]XCUITest with PACT not Returning Mock Network Response

I am attempting to run an XCUITest using PACT Consumer Swift and no matter how I try to configure it, a real network request is made instead of receiving the mocked response with a 200 status code and X-Auth-Token. 我正在尝试使用PACT Consumer Swift运行XCUITest,无论我如何配置它,都会发出真实的网络请求,而不是接收带有200状态代码和X-Auth-Token的模拟响应。 What am I missing? 我错过了什么?

Here is the UI test case: 这是UI测试用例:

import XCTest
import PactConsumerSwift
import Nimble

class MyFeature: XCTestCase {

    override func setUp() {
        super.setUp()       
        let app = XCUIApplication()
        app.launchArguments = ["FeatureTest"]
        app.launch()
        Nimble.AsyncDefaults.Timeout = 60
    }

    override func tearDown() {
        super.tearDown()
    }

    func testSuccessfulSignIn() {
        let API = MockService(
            provider: "Sign In API",
            consumer: "Sign In Consumer")

        API
            .given("user signs in with valid credentials")
            .uponReceiving("valid credentials")
            .withRequest(method: .POST,
                path: "/login",
                body: [
                    "username": "john",
                    "password": "1234"
                ])
            .willRespondWith(
                status: 200,
                headers: [
                    "X-Auth-Token":"Super-Cool-Token"
                ])

        API.run { (testComplete) in
            let app = XCUIApplication()

            let usernameField = app.textFields["UsernameField"]
            usernameField.tap()
            usernameField.tap()
            usernameField.typeText("john")

            let passwordField = app.secureTextFields["PasswordField"]
            passwordField.tap()
            passwordField.typeText("1234")

            app.buttons["LOGIN"].tap()

            expect(app.staticTexts["Welcome!"].exists).to(beTrue())

            testComplete()
        }
    }

}

Here is the actual login logic: 这是实际的登录逻辑:

func attemptLogin(_ user:String? = nil, password:String? = nil) -> Promise<LoginResponse> {
    if let
        baseUrl = URLService.getBaseUrl(),
        let url = URL(string: baseUrl + "login")
    {
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

        if 
            let username = encode(string: user),
            let password = encode(string: password)
        {
            request.httpBody = "username=\(username)&password=\(password)".data(using: String.Encoding.utf8)
        }
    }

    // Sends request and does other stuff...
}

Instantiating the MockServer class starts up a mock server in the background. 实例化MockServer类会在后台启动模拟服务器。 The url that it is running on will be available on the MockServer instance. 它运行的url将在MockServer实例上可用。 In your example this will be API.baseUrl 在您的示例中,这将是API.baseUrl

You need to configure the app under test to talk to this url (ie you want URLService.getBaseUrl() to be returning the url of the mock server). 您需要配置测试中的应用程序以与此URL通信(即您希望URLService.getBaseUrl()返回模拟服务器的URL)。 Maybe you can pass the url in via launch arguments or the environment, eg app.launchArguments = ["FeatureTest", API.baseUrl] 也许你可以通过启动参数或环境传递url,例如app.launchArguments = ["FeatureTest", API.baseUrl]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM