How to use testStubRethrowingNonReturningMethod_rethrowsError_explicitSyntax method of StubbingThrowingErrorsTests class

Best Mockingbird code snippet using StubbingThrowingErrorsTests.testStubRethrowingNonReturningMethod_rethrowsError_explicitSyntax

Run Mockingbird automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

StubbingThrowingErrorsTests.swift

Source: StubbingThrowingErrorsTests.swift Github

copy
1//
2//  StubbingThrowingErrorsTests.swift
3//  MockingbirdTests
4//
5//  Created by typealias on 7/25/21.
6//
7
8import Foundation
9import Mockingbird
10@testable import MockingbirdTestsHost
11import XCTest
12
13class StubbingThrowingErrorsTests: BaseTestCase {
14  
15  struct FakeError: Error {}
16  
17  var throwingProtocol: ThrowingProtocolMock!
18  var throwingProtocolInstance: ThrowingProtocol { return throwingProtocol }
19  
20  var rethrowingProtocol: RethrowingProtocolMock!
21  var rethrowingProtocolInstance: RethrowingProtocol { return rethrowingProtocol }
22  
23  override func setUp() {
24    throwingProtocol = mock(ThrowingProtocol.self)
25    rethrowingProtocol = mock(RethrowingProtocol.self)
26  }
27
28  func testStubThrowingMethod_returnsValue() {
29    given(throwingProtocol.throwingMethod()) ~> true
30    XCTAssertTrue(try throwingProtocolInstance.throwingMethod())
31    verify(throwingProtocol.throwingMethod()).returning(Bool.self).wasCalled()
32  }
33  func testStubThrowingMethod_throwsError() {
34    given(throwingProtocol.throwingMethod()) ~> { () throws -> Bool in throw FakeError() }
35    XCTAssertThrowsError(try throwingProtocolInstance.throwingMethod() as Bool)
36    verify(throwingProtocol.throwingMethod()).returning(Bool.self).wasCalled()
37  }
38  func testStubParameterizedThrowingMethod_throwsError() {
39    given(throwingProtocol.throwingMethod(block: any())) ~> { _ in throw FakeError() }
40    XCTAssertThrowsError(try throwingProtocolInstance.throwingMethod(block: { true }))
41    verify(throwingProtocol.throwingMethod(block: any())).wasCalled()
42  }
43  func testStubParameterizedThrowingMethod_implicitlyRethrowsError() {
44    given(throwingProtocol.throwingMethod(block: any())) ~> { _ = try $0() }
45    XCTAssertThrowsError(try throwingProtocolInstance.throwingMethod(block: { throw FakeError() }))
46    verify(throwingProtocol.throwingMethod(block: any())).wasCalled()
47  }
48  
49  func testStubThrowingMethod_returnsValue_explicitSyntax() {
50    given(throwingProtocol.throwingMethod()).willReturn(true)
51    XCTAssertTrue(try throwingProtocolInstance.throwingMethod())
52    verify(throwingProtocol.throwingMethod()).returning(Bool.self).wasCalled()
53  }
54  func testStubThrowingMethod_throwsError_explicitSyntax() {
55    given(throwingProtocol.throwingMethod()).returning(Bool.self).willThrow(FakeError())
56    XCTAssertThrowsError(try throwingProtocolInstance.throwingMethod() as Bool)
57    verify(throwingProtocol.throwingMethod()).returning(Bool.self).wasCalled()
58  }
59  func testStubParameterizedThrowingMethod_throwsError_explicitSyntax() {
60    given(throwingProtocol.throwingMethod(block: any())).willThrow(FakeError())
61    XCTAssertThrowsError(try throwingProtocolInstance.throwingMethod(block: { true }))
62    verify(throwingProtocol.throwingMethod(block: any())).wasCalled()
63  }
64  func testStubParameterizedThrowingMethod_implicitlyRethrowsError_explicitSyntax() {
65    given(throwingProtocol.throwingMethod(block: any())).will { _ = try $0() }
66    XCTAssertThrowsError(try throwingProtocolInstance.throwingMethod(block: { throw FakeError() }))
67    verify(throwingProtocol.throwingMethod(block: any())).wasCalled()
68  }
69  
70  func testStubRethrowingReturningMethod_returnsValue() {
71    given(rethrowingProtocol.rethrowingMethod(block: any())) ~> true
72    XCTAssertTrue(try rethrowingProtocolInstance.rethrowingMethod(block: { throw FakeError() }))
73    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Bool.self).wasCalled()
74  }
75  func testStubRethrowingReturningMethod_returnsValueFromBlock() {
76    given(rethrowingProtocol.rethrowingMethod(block: any())) ~> { return try $0() }
77    XCTAssertTrue(rethrowingProtocolInstance.rethrowingMethod(block: { return true }))
78    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Bool.self).wasCalled()
79  }
80  func testStubRethrowingReturningMethod_rethrowsError() {
81    given(rethrowingProtocol.rethrowingMethod(block: any())) ~> { return try $0() }
82    XCTAssertThrowsError(try rethrowingProtocolInstance.rethrowingMethod(block: {
83      throw FakeError()
84    }) as Bool)
85    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Bool.self).wasCalled()
86  }
87  func testStubRethrowingNonReturningMethod_rethrowsError() {
88    given(rethrowingProtocol.rethrowingMethod(block: any())) ~> { _ = try $0() }
89    XCTAssertThrowsError(try rethrowingProtocolInstance.rethrowingMethod(block: {
90      throw FakeError()
91    }) as Void)
92    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Void.self).wasCalled()
93  }
94  
95  func testStubRethrowingReturningMethod_returnsValue_explicitSyntax() {
96    given(rethrowingProtocol.rethrowingMethod(block: any())).willReturn(true)
97    XCTAssertTrue(try rethrowingProtocolInstance.rethrowingMethod(block: { throw FakeError() }))
98    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Bool.self).wasCalled()
99  }
100  func testStubRethrowingReturningMethod_returnsValueFromBlock_explicitSyntax() {
101    given(rethrowingProtocol.rethrowingMethod(block: any())).will { return try $0() }
102    XCTAssertTrue(rethrowingProtocolInstance.rethrowingMethod(block: { return true }))
103    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Bool.self).wasCalled()
104  }
105  func testStubRethrowingReturningMethod_rethrowsError_explicitSyntax() {
106    given(rethrowingProtocol.rethrowingMethod(block: any())).will { return try $0() }
107    XCTAssertThrowsError(try rethrowingProtocolInstance.rethrowingMethod(block: {
108      throw FakeError()
109    }) as Bool)
110    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Bool.self).wasCalled()
111  }
112  func testStubRethrowingNonReturningMethod_rethrowsError_explicitSyntax() {
113    given(rethrowingProtocol.rethrowingMethod(block: any())).will { _ = try $0() }
114    XCTAssertThrowsError(try rethrowingProtocolInstance.rethrowingMethod(block: {
115      throw FakeError()
116    }) as Void)
117    verify(rethrowingProtocol.rethrowingMethod(block: any())).returning(Void.self).wasCalled()
118  }
119}
120
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)