如何在MVC中测试控制器类?

Tho*_*hor 3 model-view-controller unit-testing controller ios swift

我正在根据模型 - 视图 - 控制器模式在Swift中构建一个简单的iOS应用程序.我可以通过给出输入数据并根据我的期望断言输出结果来测试Model类.但我想知道如何测试Controller类?

似乎如果我想测试Controller类,测试逻辑会复杂得多.有没有标准的方法来测试Controller类?

sol*_*ell 6

不要测试你的UIViewControllers.有很多东西发生在他们身上,你没有看到和/或无法控制.相反,在其他对象(如View Models)中保留尽可能多的逻辑,而不是你UIViewController的.然后,您可以测试View模型,就像测试模型一样.

编辑:

您可能希望如何构建UIViewController和测试模型和视图模型:

这段代码的主要内容是:

  1. 使用依赖注入
  2. 为Release中的类提供真正的依赖关系,并在测试中提供虚假的依赖关系

查看控制器

// this class is super simple, so there's hardly any reason to test it now.
class SomeViewController: UIViewController {
    @IBOutlet weak var someLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // we give the *real* user accessor in the view controller
        let viewModel = SomeViewModel(userAccessor: DBUserAccessor())
        someLabel.text = viewModel.welcomeMessage
    }
}
Run Code Online (Sandbox Code Playgroud)

用户模型

struct User {
    var name: String
}
Run Code Online (Sandbox Code Playgroud)

用户访问者

这是View Model需要的依赖项.在发布期间(在View Controller中)提供一个真实的.在测试期间给出假的,以便您可以控制它.

protocol UserAccessor {
    var currentUser: User? { get }
}

// since we're using the fake version of this class to test the view model, you might want to test this class on its own
// you would do that using the same principles that I've shown (dependency injection).
class DBUserAccessor: UserAccessor {
    var currentUser: User? {
        // some real implementation that's used in your app
        // get the user from the DB
        return User(name: "Samantha") // so not really this line, but something from CoreData, Realm, etc.
    }
}

class FakeUserAccessor: UserAccessor {
    // some fake implementation that's used in your tests
    // set it to whatever you want your tests to "see" as the current User from the "DB"
    var currentUser: User?
}
Run Code Online (Sandbox Code Playgroud)

查看模型

这就是您想要测试的实际逻辑.

class SomeViewModel {
    let userAccessor: UserAccessor

    init(userAccessor: UserAccessor) {
        self.userAccessor = userAccessor
    }

    var welcomeMessage: String {
        if let username = self.username {
            return "Welcome back, \(username)"
        } else {
            return "Hello there!"
        }
    }

    var username: String? {
        return userAccessor.currentUser?.name
    }
}
Run Code Online (Sandbox Code Playgroud)

测试

最后,你想如何测试它.

class SomeViewModelTest: XCTestCase {
    func testWelcomeMessageWhenNotLoggedIn() {
        let userAccessor = FakeUserAccessor()
        let viewModel = SomeViewModel(userAccessor: userAccessor) // we give the *fake* user accessor to the view model in tests
        userAccessor.currentUser = nil // set the fake UserAccessor to not have a user "logged in"

        // assert that the view model, which uses whatever you gave it, gives the correct message
        XCTAssertEqual(viewModel.welcomeMessage, "Hello there!")
    }

    func testWelcomeMessageWhenLoggedIn() {
        let userAccessor = FakeUserAccessor()
        let viewModel = SomeViewModel(userAccessor: userAccessor)
        userAccessor.currentUser = User(name: "Joe") // this time, the use is "logged in"

        XCTAssertEqual(viewModel.welcomeMessage, "Welcome back, Joe") // and we get the correct message
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我的荣幸。我添加了一堆示例代码,展示了如何设置它。 (2认同)