Code Monkey home page Code Monkey logo

Comments (5)

seanhenry avatar seanhenry commented on May 24, 2024

Hi @fmirault

Thanks for raising this issue.

I can see why you would want the type to match the return type in the method but this was my reasoning for the decision:

If the type of the stub property exactly matches the return type of the method then it won't always compile. Example when method returns guaranteed value:

...
var stubbedObjectResult: Any // won't compile
func object(forKey defaultName: String) -> Any {
  ...
  return stubbedObjectResult
}

The two ways to fix that would be to either:

var stubbedObjectResult: Any!

or:

return stubbedObjectResult!

The first approach works for all return types and the second option makes the mock a little more complex.

So I opted to keep things simpler, but also more flexible. For example, if you were to change the signature of:

func object(forKey defaultName: String) -> Any?

to:

func object(forKey defaultName: String) -> Any

then the mock wouldn't have to be regenerated.

from swiftmockgeneratorforxcode.

fmirault avatar fmirault commented on May 24, 2024

Hi @seanhenry

Thank you for your answer, now I better understand your choice :)
It's not a huge problem, because my use case is that I want to test some methods with nil values, so I just have to change some generated code after using the generator, not the best practice because every time I use the generator I loose my changes, but in every case I save time thanks to your tool. Thank you very much !

from swiftmockgeneratorforxcode.

seanhenry avatar seanhenry commented on May 24, 2024

No problem. I'm glad you like it :)

Do you mean you need your mock method to return nil? If so, you don't need to change the generated mock because it will return nil by default because the IUO will be converted to an optional.

Example:

class MyMock: MyType {
  ...
  var stubbedObjectResult: Any!
  func object(forKey defaultName: String) -> Any? {
    ...
    return stubbedObjectResult
  }
}

let mock = MyMock()
mock.object(forKey: "anyKey") == nil // true

from swiftmockgeneratorforxcode.

fmirault avatar fmirault commented on May 24, 2024

Oh you're right! I thought that I will have a crash (I don't really know why...) but it's working, so I don't have to change anything in the generated code, thank you ! 👍

from swiftmockgeneratorforxcode.

seanhenry avatar seanhenry commented on May 24, 2024

👍

from swiftmockgeneratorforxcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.