swift - Unit test wrapper for URLSession completer -


i have defined own class methods fits signature of urlsession complete callback, e. g. (data?, response?, error?) -> void. method contains common logic handling response, e. g. checking data, parsing etc. unit test method. methods contains verification, instance,

guard let data = data else {     //some logic     return } 

here test function terminated. of course not possible achieve against void return (i think so, maybe missed something). option - mark method throws, , test specific errors. method not fit urlsession.shared.datatask method. paranoid these things? there possibility achieve it? in advance.

usually try separate query logic several parts:

1) router 2) api client uses router 3) mapping model

and parts can test.

how can test api client:

fileprivate func testperformanceofgetnewsfromapi() {          let expectationtimeout: double = 30.0          self.measure {              let expectation = self.expectation(description: "get gifters")              newsapiclient.getnews(closure: { response in                 expectation.fulfill()             })              self.waitforexpectations(timeout: expectationtimeout) { error in                 xctassertnil(error)             }         }     } 

this test check. apiclient receive response within 30 seconds.

how can test mapping:

for mapping, use jason: https://github.com/delba/jason

setup swift file:

import xctest import jason @testable import projectname  final class newstests: xctestcase {      // mark: - properties     fileprivate var news: news!      // mark: - lyfecycles     override func setup() {         super.setup()          news = mockexample()     }      override func teardown() {          news = nil         super.teardown()     } } 

then, create in class mock:

 fileprivate func mockexample() -> examplemodel? {          let data: data          let json: json          {             try data = data(resource: "myexamplefile.json")  // here enter json example file. target member ship file should test target              try json = jsonserialization.jsonobject(with: data, options: jsonserialization.readingoptions()) as! json          } catch let error {             xctfail(error.localizeddescription)             return nil         }          let model = examplemodel(json: json)          return model     } 

then, can write test in class:

fileprivate func testmapping() {          xctassertnotnil(news)         xctassertequal(news.title, mockexample()?.title)         xctassertequal(news.text, mockexample()?.text)         xctassertequal(news.timestamp, mockexample()?.timestamp)     } 

in testing logic, can add image uploads (if present in json). thus, can check if current model correct you, can process json response.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -