gpt4 book ai didi

ios - 对象的单元测试

转载 作者:行者123 更新时间:2023-11-29 02:22:38 24 4
gpt4 key购买 nike

我是 objective c 的学习者,开始做单元测试,

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

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

但是,这个测试从未运行我知道我在这里犯了一些错误我遗漏了一些东西但无法弄清楚在这种情况下有人可以帮助我

最佳答案

Xcode 将只运行方法签名以您拥有的“test”开头的测试,而且方法签名可以没有参数。如果将方法名称更改为

,测试将运行
- (void)testModelObject
{

}

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

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

关于ios - 对象的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27955213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com