Questions tagged [xctestexpectation]

In Xcode 6, the XCTestExpectation class is a built-in support for asynchronous testing.

In Xcode 6, the XCTestExpectation class is a built-in support for asynchronous testing.

Useful link

62 questions
3
votes
1 answer

XCTest: Check That a Callback is Not Called

I know how to wait for a callback with expectations in XCT. However, what about testing for the inverse? Below is an example of a test that I have as part of my tests: manager.state = .initialized let exp = expectation(description:…
Stunner
  • 12,025
  • 12
  • 86
  • 145
3
votes
1 answer

How to test async functions in swift using XCTWaiter and exceptions

I want to test asynchronous functions in Swift, hence as shown below, I created a XCTestExpectation and passed it on to an XCTWaiter. Now, irrespective if the expectation is fulfilled or not, I get a test ran successfully always. Can you point out…
rahulg
  • 2,183
  • 3
  • 33
  • 47
3
votes
1 answer

How to write unit test for `downloadTask` with `completionHandler `?

Here is my downloading function: // Download a file from the url to the local directory class func downloadUrl(url: URL, to dirUrl: URL, completion: (() -> ())?){ let sessionConfig = URLSessionConfiguration.default let session =…
Jay Wang
  • 2,650
  • 4
  • 25
  • 51
3
votes
1 answer

Is it safe to call XCTestExpectation fulfill method on background thread?

I am using XCTestExpectation in a lot of tests and sometimes (very randomly) some expectations are not fulfilled (although I am sure they should be). While investigating this problem I have noticed that some expectations are fulfilled in a main…
Piotr
  • 5,543
  • 1
  • 31
  • 37
3
votes
1 answer

XCTest and using mock objects as notification receivers

In XCTest with swift you can define mock objects in the test function you need it in. Like so func testFunction(){ class mockClass: AnyObject{ func aFunction(){ } } } I'm trying to use these mock objects to test that another…
AndyRyan
  • 1,500
  • 12
  • 23
2
votes
0 answers

How can I get a nested objective-c async method to execute under test using mocks and expectations?

Here is my test - (void)test_IsOverLimit { id myCoordinator = OCMPartialMock([[MyCoordinator alloc] init]); XCTestExpectation *expectationLimitResult = [self expectationWithDescription:@"my limit result"]; [myCoordinator…
Jamie M.
  • 590
  • 1
  • 8
  • 16
2
votes
2 answers

Wait for XCTestExpectation before starting next unit test in XCode

I have a number of asynchronous unit tests which work correctly on their own using expectations. However when I run all the tests in the suit, they do not wait for each other to complete - the asynchronous callbacks are still pending when the next…
deepwinter
  • 4,568
  • 2
  • 31
  • 37
2
votes
2 answers

XCTKVOExpectation times out even though element exists

I am using swift, xcode 8.3.3, and XCTest. I am trying to wait for an element to exist on the screen using XCTKVOExpectation. It is always returning the result of timedout (2) even though the element exists. Here is my code: func…
reutsey
  • 1,743
  • 1
  • 17
  • 36
2
votes
2 answers

How to capture Failure logs while testing : XCTest?

Working with XCTest. While testing testresults are saved in TestSummaries.plist at path: /Users/smriti/Library/Developer/Xcode/DerivedData/Project_name/Logs/Test/ Can anyone tell how to read contents of TestSummaries.plist file from above path? Need…
Smriti10
  • 58
  • 8
2
votes
1 answer

How to use expectationForNotification

I've tried everything, but the only way I could get a successful test is to actually send the notification in the test function, which kinda defeats the purpose. I have a button. When I tap the button, it sends a notification. How can I use…
2
votes
1 answer

IOS: Unit testing using dispatch_async is not executing

I have written a script similar to below code. I kept breakpoint inside the "dispatch_async", but it is not getting executed. What could be the issue here? dispatch_async(dispatch_get_main_queue()) { let array = response!.allObjects as!…
Coder
  • 1,661
  • 4
  • 27
  • 50
2
votes
0 answers

iOS Testing asynchronous method performance

I'm trying to write a performance test for a API request method. When I run it without measureBlock it runs ok, but when I use it as performance test I got the error message: *** Assertion failure in -[Performance…
Christian
  • 382
  • 2
  • 11
1
vote
3 answers

How to test method that dispatch work asynchronously in Swift

I need to write the Unit Test for the following method func setLabelText(msg: String) { DispatchQueue.main.async { self.label.text = msg } }
Sajal Gupta
  • 147
  • 2
  • 14
1
vote
1 answer

XCTest Async Function Swift

i'm quite new to XCTest, i have structured my code in Model, View, Controller So controller will take data from Model and once got data, Controller will update View. So i have my Controller and view as follow Controller: func loadData() { …
wes. i
  • 627
  • 9
  • 26
1
vote
0 answers

XCTest Completion Handler Assertion Retain Cycle

I have been writing Xcode tests using the XCTest framework for a while, mostly async tests of the getters of a service with completion handlers of the following format with no issues: XCTestExpectation *promise = [self…
Aleksander
  • 2,735
  • 5
  • 34
  • 57