模拟NSManagedObjectContext时出错

时间:2015-09-24 11:17:15

标签: swift nsmanagedobjectcontext

我试图模拟NSManagedObjectContext,以便在调用executeFetchRequest时返回模拟用户。

class MockNSManagedObjectContext: NSManagedObjectContext {

        override func executeFetchRequest(request: NSFetchRequest) throws -> [AnyObject] {
        let mockUser: User = User()
        mockUser.usDisplayName = "Mock User"
        mockUser.usUserID = "MockUserId"

        return [mockUser]
    }
}

我在像这样的数据管理器类上单元测试方法

func test_When_getUserByEmail_Is_Called_It_Returns_A_User_If_Found() {

    let mockManagedObjectContext = MockNSManagedObjectContext()
    let arUserDataHelper = ARUserDataHelper(managedObjectContext: mockManagedObjectContext)
    let returnValue = arUserDataHelper.getUserByEmail("MockUserId")

    XCTAssert(returnValue != nil)

}

编辑:添加了ARUserDataHelper类

class ARUserDataHelper {

   let managedObjectContext: NSManagedObjectContext

   init(managedObjectContext: NSManagedObjectContext){
    self.managedObjectContext = managedObjectContext
   }

   func getUserByEmail(email: String) -> User? {
       let fetchRequest = NSFetchRequest(entityName: "User")
       fetchRequest.predicate = NSPredicate(format: "usUserID == %@", email)
       if let fetchResults = try! self.managedObjectContext.executeFetchRequest(fetchRequest) as? [User] {
          return fetchResults.count > 0 ? fetchResults[0] : nil
    }
    else {
        return nil
    }
}

}

当我运行测试时,我收到错误

  

失败:捕获" NSInvalidArgumentException"," - [ARRO.User   setUsDisplayName:]:       无法识别的选择器发送到实例0x7fd2c2dbfc50"

在executeFetchRequest

中设置User的属性时
mockUser.usDisplayName = "Mock User"

我目前有.NET背景和学习iOS开发,因此不确定从何处开始。

有什么想法吗?

0 个答案:

没有答案