对象的单元测试 [英] Unit testing of Object

查看:39
本文介绍了对象的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是目标 c 的学习者,并开始进行单元测试,

I am a learner of objective c and struck up doing unit testing,

我想在对象下面进行单元测试

i want to unit test below object

@interface Media : NSObject{

}
@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) NSString *sex;
@property (nonatomic, readonly) NSString *Description;

- (instancetype)initWithDictionary:(NSDictionary *)mediaData;

@end

#import "Media.h"

@interface Media()

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *sex;
@property (nonatomic, strong) NSString *Description;

@end

@implementation Media

- (instancetype)initWithDictionary:(NSDictionary *)mediaData
{
    self = [super init];
    if (self)
    {
        _name; = mediaData[Name];//getting from Json
        _sex = mediaData[Sex];
        _description = mediaData[Description];
    }
    return self;
}
@end

我的测试班

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "Media.h"

@interface ModelUnitTest : XCTestCase

@end

@implementation ModelUnitTest

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testModelObject:(id)file
{
    XCTAssertNotNil(file);
    XCTAssertTrue(file isKindOfClass:[Media class]]);
    Media * fileObj = (Media *)file;
    XCTAssertNotNil(fileObj.name);
    XCTAssertNotNil(fileObj.sex);
    XCTAssertNotNil(fileObj.description);
}

但是,这个测试永远不会运行我知道我在这里犯了一些错误我遗漏了一些东西但无法弄清楚在这种情况下谁能帮助我

but, this test never runs I know i am committing some mistake here i am missing some thing but can't figure it out can anyone help me in this case

推荐答案

Xcode 将只运行具有以您拥有的 'test' 开头的方法签名的测试,但方法签名也可以没有参数.如果您将方法名称更改为

Xcode will only run tests that have a method signature that starts with 'test' which you have, but also the method signature can have no arguments. The test will run if you change the method name to

- (void)testModelObject
{

}

然而,这意味着您将不再拥有 file.您应该在方法中或在 setup 方法中初始化它,如下所示:

However that means you'll no longer have your file. You should initialize it within the method or in the setup method like so:

@interface ModelUnitTest : XCTestCase
@property (nonatomic, strong) id file;
@end

@implementation ModelUnitTest

- (void)setUp {
[super setUp];
self.file = //setup your file
}

- (void)tearDown {
    // tear down your file if necessary
    [super tearDown];
}

- (void)testModelObject
{
    XCTAssertNotNil(self.file);
    XCTAssertTrue(self.file isKindOfClass:[Media class]]);
    Media * fileObj = (Media *)self.file;
    XCTAssertNotNil(fileObj.name);
    XCTAssertNotNil(fileObj.sex);
    XCTAssertNotNil(fileObj.description);
 }

这篇关于对象的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆