Unit testing of Object

旧巷老猫 提交于 2019-12-12 04:56:02

问题


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

my test class

#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


回答1:


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
{

}

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);
 }


来源:https://stackoverflow.com/questions/27955213/unit-testing-of-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!