1
我試圖用PACT Consumer Swift運行一個XCUITest不管我如何努力進行配置,真正的網絡請求,而不是接收與200個狀態碼和X-嘲笑響應驗證令牌。我錯過了什麼?XCUITest與PACT沒有返回模擬網絡響應
這裏是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()
}
}
}
下面是實際的登錄邏輯:
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...
}
謝謝。這確實有幫助。我們有一個xcconfig文件,該文件已經具有模擬服務器運行的baseUrl並且UrlService用於獲取其基本URL。現在我的挑戰是讓配置條件變量賦值工作: - / – Adam