typealiased / mockingbird

A Swifty mocking framework for Swift and Objective-C.

Home Page:https://mockingbirdswift.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mocking an Optional property that unwraps to nil raises NSInvalidArgumentException

molenick-mov opened this issue · comments

New Issue Checklist

Description

I'm trying to mock an object property that has a type of String?. When I test the behavior when the optional unwraps to a string, everything works as expected. However, when I test the behavior when the optional unwraps to nil, I get a NSInvalidArgumentException at test runtime.

Framework Bugs

class User {
  var email: String?

  func hasEmail() -> Bool {
    if email == nil {
      return false
    } else {
      return true
    }
  }
}
let user = mock(User.self)
given(user.email).willReturn(nil)

...

expect(user.hasEmail()).to(equal(false))

Result:

Exception	NSException *	"-[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0x108c6bfb0"	0x0000600003b6d230

I read through the documentation and some of the code, and don't see how mock an optional property that unwraps to nil. What am I missing?

Environment

  • Mockingbird CLI version (mockingbird version)
  • Xcode and macOS version (Xcode 13.1, MacOS 11.6)
  • Swift version (5.5.1)
  • Installation method (SPM)
  • Unit testing framework (XCTest, Quick + Nimble)
  • Does your project use .mockingbird-ignore? No
  • Are you using supporting source files? No

I’m able to repro this specifically for dynamic mocks (Obj-C or @objc Swift classes), but Swift optionals seem unaffected.