Hello!
So I have a function that will return json from a file:
func JSONFixtureWithName(_ name: String) -> (data: Data, json: JSON) {
let path = Bundle(for: type(of: self)).url(forResource: name, withExtension: "json")!
let data = try! Data(contentsOf: path)
let json = JSON(data: data)
return (data: data, json: json)
}
And this function works properly when I have tested it using XCTest instead of Quick.
However it seems once this function is called inside of override func spec() that the path constant unwraps nil.
My file system currently looks like:
= [App]
= [Tests]
== [Folder Containing File]
=== File.json
== FileContainingProblematicMethod.swift
== FileCallingProblematicMethod.swift
And the code calling the method:
...
class NetworkTests: QuickSpec {
var spine: Spine!
var HTTPClient: CallbackHTTPClient!
override func spec() {
beforeEach {
self.HTTPClient = CallbackHTTPClient()
EventAPIHelper.spine = Spine(baseURL: URL(string:"http://example.com")!, networkClient: self.HTTPClient)
Spine.setLogLevel(.info, forDomain: .spine)
}
}
}
class FindByIDTests: NetworkTests {
/* The Following block will crash */
override func spec() {
let fixture = JSONFixtureWithName("SingleEvent")
}
/* The following block DOES NOT crash */
func testItShouldSucceed() {
let fixture = JSONFixtureWithName("SingleEvent")
}
}
I'm aware the syntax is sort of mixed, I was slowly adapting XCTest syntax into Quick syntax until the error appeared.
Any ideas? Thoughts on how to avoid the error?
Hi @Croge32! I've tried to reproduce this in my own environment but I'm not seeing the problem. Would you be able to refactor this test into your a new (isolated) project and include that with the dependencies in a zip file here? It'll allow us to investigate more. Cheers!
@istx25 I'm having trouble getting my project refactored into a new project to copy over for you, since I need to delete a lot of files (it's a company project). Do you know an easy process to do this or can I get you information another way?
I just need this test to run and reproduce the problem in isolation to prevent side effects.
If you'd like to share the code in a more private setting, feel free to email it directly to me at willow.[email protected].
@istx25 Emailing you a zip now! I copied the project over and removed all source code and other tests so it's about as isolated as it can get I think.
Thanks! I'll look over the email and get back to you.
Fixed in private message, thanks!
For everyone else, the problem had to do with the fixtures being in an external blue directory instead of being directly added to the project and grouped together.
How to fix:
1 - open your Xcode project
2 - find fixtures directory in Xcode file list
3 - grab the three *.json and pull them out of the blue directory
4 - delete the blue directory
5 - create new group called Fixtures
6 - move the *json files into fixtures directory
7 - run tests again
_the problem:_

_the solution:_
