[英]Access an asset catalog for unit tests in Objective-C
I have a CocoaPods project written in Objective-C that I would like to run unit tests against. 我有一个用Objective-C编写的CocoaPods项目,我想对它进行单元测试。 The project in question works with images, so I need to feed some images to it for testing purposes. 有问题的项目适用于图像,因此我需要向其提供一些图像以进行测试。 Under no circumstances will these images be used in a release version - so they do not need to be accessible for the main target. 在任何情况下,这些图像都不会在发行版本中使用-因此,主要目标不需要访问它们。
I started by creating an assets file & associated it with my test target: 我首先创建资产文件并将其与测试目标相关联:
Inside my assets file, I have created an entry for my first test image: 在资产文件中,我为第一个测试图像创建了一个条目:
I have also verified that the assets file is being copied into the bundle resources under the test object project phases tab: 我还验证了资产文件已被复制到测试对象项目阶段标签下的捆绑资源中:
Then, in my unit test I call it: 然后,在我的单元测试中,我称之为:
- (void)testLoadImage {
UIImage *image = [UIImage imageNamed:@"colortestpattern"];
XCTAssertNotNil(image);
}
The test fails, as the image is not set. 测试失败,因为未设置图像。
Because I will need a large collection of images for testing, I was hoping to use the asset file. 因为我需要大量的图像进行测试,所以我希望使用资产文件。 If however, this is not possible, then I'm open to alternatives. 但是,如果这不可能,那么我愿意接受替代方案。
Have you tried UIImage
's class method imageNamed:inBundle:compatibleWithTraitCollection:
? 您是否尝试过UIImage
的类方法imageNamed:inBundle:compatibleWithTraitCollection:
?
UIImage *image = [UIImage imageNamed: @"colortestpattern"
inBundle: [NSBundle bundleForClass: [self class]]
compatibleWithTraitCollection: nil];
The image should be in the unit test bundle, not the main bundle. 该映像应位于单元测试包中,而不是主包中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.