Maybe a duplicate, however; the accepted answer did not answer my question: How to fake Realm Results for tests
I want to return a fake Realm 'Results' object on my mocked object when unit testing my View Controller. (Similar to how it can be done with Moq in C#)
Is it possible to create a Realm Results object consisting of my test data without creating an In-Memory Realm database, adding the required data into it & then querying it for the objects?
Mock Object:
class MockRealmRepository: RealmRepository {
override init() {}
var getAccountRolesCallCount = 0
var getAccountRolesReturnValue: Results<AccountRole>!
override func getAccountRoles() -> Results<AccountRole> {
getAccountRolesCallCount += 1
return getAccountRolesReturnValue
}
}
Unit Test implementation of Mock:
class CreateProfileViewControllerTests: XCTestCase {
private var mockRealmRepository: MockRealmRepository!
override func setUp() {
super.setUp()
mockRealmRepository = MockRealmRepository()
}
func testCheckUsersRole_Valid() {
let admin = AccountRole(role: "Admin")
let user = AccountRole(role: "User")
let guest = AccountRole(role: "Guest")
// Create a Results<AccountRole>() containing the above AccountRole objects
mockRealmRepository.getAccountRolesReturnValue = // Assign my Results<AccountRole> object here
}
}