Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change fetch function from network layer to one with an escaping closure #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

emmanuelkolawole4
Copy link

test FeedDataManager
test models gotten from network call can be mapped to cell view models

test FeedDataManager
test models gotten from network call can be mapped to cell view models
enum TweetAPIError: Error {
case noData
case response
case parsingData
}

class FeedDataManager: FeedDataManagerProtocol {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any class that does not implement inheritance should be defined as final class.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good practice to define all classes as final when there is no inheritance. This help to the compiler's performance.

}


let decoder = JSONDecoder()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is better to pass the JSONDecoder instance as a parameter instead of creating it inside this method. If you do that, you are applying dependency injection because the JSONDecoder is a dependency for your method.

let url = URL(string: "https://gist.githubusercontent.com/ferdelarosa-wz/0c73ab5311c845fb7dfac4b62ab6c652/raw/6a39cffe68d87f1613f222372c62bd4e89ad06fa/tweets.json")
guard let url else { return }

let request = URLRequest(url: url)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the request should be a parameter for the fetch method. I understand that it is just one url for this project but I just trying to do this in a general way.

}

do {
let jsonData = try decoder.decode(TweetsResponse.self, from: data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only works for the TweetsResponse. Here you can define a generic method to do the data decoding. Just like this:

func decode<T: Decodable>(data: Data?, decoder: JSONDecoder = JSONDecoder()) -> T? {
    return try? decoder.decode(T.self, from: data ?? Data())
}

import XCTest
@testable import MiniBootcamp

class FeedDataManagerTests: XCTestCase {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it's good to define final the classes for the test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants