Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBDesignable Errors When Adding to Tests Target

Tags:

I have a simple UIButton subclass that implements IBDesignable with an IBInspectable var:

@IBDesignable class Button: UIButton {
    @IBInspectable var borderColor: UIColor = UIColor.whiteColor() {
        didSet { layer.borderColor = borderColor.CGColor }
    }
}

I am not using this within a framework and it is working in Interface Builder as intended, however, once I add this subclass to my Tests target, it stops rendering live and I get the following errors:

Main.storyboard: error: IB Designables: Failed to update auto layout status: dlopen(TestTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest
Referenced from: TestTests.xctest
Reason: image not found

Main.storyboard: error: IB Designables: Failed to render instance of Button: dlopen(TestTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest
Referenced from: TestTests.xctest
Reason: image not found

If I remove IBDesignable and the IBInspectable vars, the errors go away - unfortunately so does the live rendering in Interface Builder.

How do I test against an IBDesignable class without these errors?

like image 661
Adolfo Avatar asked Sep 25 '14 05:09

Adolfo


2 Answers

At first, I thought this was a kind of bug in Xcode. Following is the workaround I found:

STEP 1

Mark your class and properties as public.

@IBDesignable public class Button: UIButton {     @IBInspectable public var borderColor: UIColor = UIColor.whiteColor() {         didSet { layer.borderColor = borderColor.CGColor }     }      @IBInspectable public var borderWidth:CGFloat = 0.0 {         didSet { layer.borderWidth = borderWidth }     } } 

STEP 2

Import your application module from your "Tests" module.

For example, assuming that your application is named MyGreatApp, in your MyGreatAppTests/MyGreatAppTests.swift:

import UIKit import XCTest import MyGreatApp  class MyGreatAppTests: XCTestCase {      func testExample() {         let btn = Button()         btn.borderColor = UIColor.redColor()         XCTAssertEqual(UIColor(CGColor:btn.layer.borderColor), UIColor.redColor(), "borderColor")     } } 

You don't need to add 'Button.swift' to your "Tests" target.

STEP 3 (for Swift)

In your storyboard explicitly select the module MyGreatApp for any custom classes instead of letting Xcode use the current module.

Interface Builder select main target module

like image 167
rintaro Avatar answered Nov 12 '22 00:11

rintaro


Your question describes exactly the circumstances I experienced. The error is also visible in the attribute inspector under your own attributes.

This is the workaround that worked for me:

Step 1 remove all @IBDesignable and @IBInspectable from your source code

Step 2 Go back to the interface builder. The error is still there.

Step 3 Restart XCode, rebuild your project. Errors should be vanished.

Step 4 add all @IBDesignable and @IBInspectable again to your source code

After this steps I was able to go on with my project without any problems.

My theory why this works is that the interface builder caches some stuff that is not deleted (and rebuilt later) when you do a Project -> Clean.

The other answer (Importing your main module into your test module) is a good idea and solved some nasty problems for me in the past but not this one.

like image 40
Gerd Castan Avatar answered Nov 12 '22 01:11

Gerd Castan