Tag: unit testing

如何在Swift中unit testing这个自定义的UITextField?

我已经创build了一个像这样的自定义UITextField import Foundation import UIKit class NoZeroTextField: UITextField, UITextFieldDelegate { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.delegate = self } func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if (string == "0" ) { //ignore input return false } return true } } 我正在尝试编写类的unit testing,但问题是与NSCoder实例传递给构造函数。 我无法实例化或将其设置为零。 我怎样才能unit testing这个类?

OCMock通过任何CGSize

我正在使用OCMock,我试图在我的一个testing中做到这样的事情: [[mockScrollView expect] setContentSize:[OCMArg any]]; 问题是[OCMArg any]返回一个idtypes,我想使用任何CGSize ,因为我不知道它的确切值。 我怎么能通过这个论点?

app store的unit testing

林新编写iOS应用程序,几乎完成了我的第一个游戏写作。 我已经在线阅读了关于unit testing的内容,并且明白从一开始我就应该从头开始编码(呃哦…)。 我的应用程序大约有4000行代码,包括8个类和约50个方法。 我应该关心unit testing吗? 究竟是什么? 在我提交给App Store之前,我必须这样做吗? 任何答案或有用资源的链接将不胜感激。 谢谢!

MagicalRecord完成块在testing目标下不被调用

MagicalRecord.saveWithBlock({ context in if let items = dictionary["items"] as? Array<NSDictionary> { for itemInfo in items { DBItem.findOrUpdateItemWithDictionary(itemInfo, inContext: context) } } //is called if let sets = dictionary["item_sets"] as? Array<NSDictionary> { for setInfo in sets { DBSet.findOrUpdateSetWithDictionary(setInfo, inContext: context) } } }, completion: { finished, error in completionBlock(error) //is not called }) 这是我如何设置我的核心数据堆栈: MagicalRecord.setupCoreDataStackWithInMemoryStore()

用台风注入模拟

我正在写XCTest,并用Typhoon注入嘲弄的依赖。 这里是我的ViewController代码: – (instancetype)init { self = [super init]; MDMainAssembly *assembly = (MDMainAssembly *) [TyphoonComponentFactory defaultFactory]; self.alertManager = [assembly alertManager]; return self; } 这是我如何改变注射: self.mockedAlertManager = mock([MDAlertManager class]); MDMainAssembly *assembly = [MDMainAssembly assembly]; TyphoonComponentFactory *factory = [TyphoonBlockComponentFactory factoryWithAssembly:assembly]; TyphoonPatcher *patcher = [[TyphoonPatcher alloc] init]; [patcher patchDefinition:[assembly alertManager] withObject:^id { return self.mockedAlertManager; }]; [factory attachPostProcessor:patcher]; 然而,testing失败,因为这个工厂不可能设置为默认。 […]

为什么我应该使用一个单独的testing目标来运行XCTests,我应该怎么做?

我曾经问过一个与XCTests有关的问题 。 在其中一个答案中,我被告知,在运行unit testing时(至less在iOS开发中),使用单独的testing目标(除主应用程序之外)是一种常见的做法。 我试图find一些关于它的消息来源,但我不能 我明白,这可能是一个最好的做法,所以我真的很想了解它。 有人可以向我解释为什么它很重要,我从中得到什么好处,我应该怎么做呢? 链接到一些文章解释这个问题将不胜感激。 PS我明白,我需要testing的特殊环境(假内存数据库,模拟networking层等),但直到现在我设法实现它没有一个单独的testing主机。 但我相信可能有更好的办法。

检查运行的代码是否是unit testing用例

我想检查运行的代码是否是unit testing的情况,或者是否对结果执行不同的代码,例如: if ( unit test case is running ) { do something } else { do other thing } 任何想法呢?

OCMock自动使用testing代码中的模拟实例吗?

我是OCMock的新手 。 我使用dispatch_once()创build了一个单例类MyManager : @implementation MyManager + (id)sharedInstance { static MyManager *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager; } 我在School课上有一个使用上述单例的方法: @implementation School … – (void) createLecture { MyManager *mgr = [MyManager sharedInstance]; [mgr checkLectures]; … } @end 现在,我想unit testing这个方法,我使用了MyManager的部分模拟: – (void) testCreateLecture { // create a […]

为什么使用Quick进行unit testing时多次调用before-和afterEach块?

我写了一个包含beforeEach和afterEach示例组的afterEach 。 而且我预计每个beforeEach和afterEach每个都会被调用一次。 唉,对于一个单一的it beforeEach和afterEach被多次调用。 我看了一些文档(即Quick的自己的文档和http://jasmine.github.io/2.1/introduction.html ),但这些都没有帮助我的原因。 这是一个小小的片段,演示了这一点: 类CheckerTests:QuickSpec { override func spec() { describe("something") { beforeEach { tLog.info("describe before") } afterEach { tLog.info("describe after") } context("of something") { beforeEach { tLog.info("context before") } afterEach { tLog.info("context after") } it("should behave like something") { tLog.info("in the `IT`") expect(true).to(beTrue()) } } } } } 我的控制台日志: 上面的日志提出了两个问题: […]

目标不止一次地连接在一起

我有两个目标app和appTests 。 另外我还有和这两个目标挂钩的Wine和框架Realm和RealmSwift。 有没有例外当我在类app使用类Wine 。 但是,当我想运行testing appTests.swift (22行) import UIKit import XCTest import RealmSwift class appTests: XCTestCase { func testRealmAdd() { NSFileManager.defaultManager().removeItemAtPath(Realm.defaultPath, error: nil) let realm = Realm() let wine = Wine() // when error occure wine.photo = "photo" wine.desc = "description" wine.raiting = 3.0 realm.write { () -> Void in realm.add(wine) } let result […]