问题
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