Title: How can I mock SectionedFetchedResults for testing in iOS?
Tags: ios;swift;core-data;swiftui
Question: I am struggling to work how I can use a protocol to represent the core data type, SectionFetchedResults to enable me to add snapshot testing for my iOS SwiftUI app.
Below is the code and I am trying to pass players into SquadView using a protocol to represent it.
What do you think I could do?
import SwiftUI
struct SquadContainerView: View {
@Environment(\.managedObjectContext) var viewContext
@SectionedFetchRequest var players: SectionedFetchResults<Int16, Player&gt;
private let team: Teamable
init(team: Teamable) {
let predicate =  NSPredicate(format: &quot;%K == %@&quot;, #keyPath(Player.team.id), team.id as CVarArg)
_players = SectionedFetchRequest<Int16, Player&gt;(sectionIdentifier: \.position, sortDescriptors: [SortDescriptor(\.shirtNumber)], predicate: predicate)
self.team = team
var body: some View {
SquadView(viewModel: .init(team: team,
playerService: PlayerService(viewContext),
teamService: TeamService(viewContext)),
players: players)
Comment: Hi @Asperi thanks again for helping out, Unfortunately I get this error when trying that `generic struct 'SquadView' requires the types 'SectionedFetchResults.Element' (aka 'SectionedFetchResults.Section') and 'Player' be equivalent`
I did look at that answer which I have used in another part of my code.

Comment: The signature of my struct was `struct SquadView: View where Results.Element == Player`

Comment: take a look at this general example https://stackoverflow.com/a/71032848/14017100

Comment: Does this answer your question https://stackoverflow.com/a/61495490/12299030?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:07/04/22', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<ios><swift><core-data><swiftui>', 'Title': 'How can I mock SectionedFetchedResults for testing in iOS?', 'ViewCount': 80, 'id': 72861424, 'user_AboutMe': '', 'user_AccountId': 8020873.0, 'user_DisplayName': 'Edward', 'user_Reputation': 2706.0, 'user_Views': 109.0, 'user_id': 6049579.0}}