2

I wonder if there is a need to create a Unit Test Case class or even a separate unit test method to test a Model of it is a Struct?

I trying to follow the TDD to create a Signup feature for my mobile app. On a piece of paper, I have sketched what I need to create:

  • ViewController class,
  • Presenter class,
  • A Validator class to validate Model field values like the email address format and the password length and etc...
  • WebService class to send HTTP Request with user details,
  • A Signup Form Request Model struct. A Model struct that will be sent over to my server.
  • A Signup Response Model struct. A Struct that will be used to convert server response into a Swift struct.

I wonder if there is a need to create a Unit Test Case class or even a separate unit test method to test a Model of it is a Struct? Structs do not have failable initializers. And because Swift struct has a memberwise initializer, there is really nothing to test...

For example. The SignupModel.swift - a separate file:

struct SignupModel {
    let firstName: String
    let lastName: String
    let email: String
    let password: String
}

Do I really need to create a test method:

    func testInit_SignupModelExists() {
       let signupModel = SignupModel(firstName:"John", lastName: "Doe", email: "[email protected]", password: "12345678")


       XCTAssertNotNil(signupModel)
    }

Do you guys create this kind of Unit tests when following the TDD?

3
  • 1
    Not relevant for TDD perhaps but a struct like this normally gets covered when writing unit test for methods that uses the struct. Commented Feb 25, 2020 at 15:09
  • @JoakimDanielson thank you! That's a good idea actually. I will double-check the Code Coverage to see if it covers files that contain a single Struct. Commented Feb 25, 2020 at 15:12
  • Great! Swift files that contain a single Struct are not even listed in the code coverage report. I am pretty confident now that in its simplest form there is no need for a test case. Commented Feb 25, 2020 at 15:16

1 Answer 1

1

You need to test your functionality with unit tests. In here, there is no any functionality.

If your init function may return nil, you may need. But in this case, your initializer never return any value but your create. So we can't say this a functionality.

Sign up to request clarification or add additional context in comments.

3 Comments

That's what I also thought... If I am to create a Unit test for a Model struct then its more like I am testing Apple's Swift language, rather than code I have written.
@Simplyi That’s correct. Don’t test Apple’s code at all, not even the Cocoa Touch framework. Test only things that you do.
Thank you, @Matt 🙏

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.