質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

305閲覧

swiftでテストが失敗する。unable to dequeue a cell with identifier ...

kaorimanabe

総合スコア13

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/06/19 12:04

cellに文字が表示されているかどうかのtestを書きたいのですが、以下のエラーでテストが失敗します。解決法を教えていただけますと幸いです。

[HogeTests.AnswerListTests 表示__一行目にLabelが表示されている] : failed: caught "NSInternalInconsistencyException", "unable to dequeue a cell with identifier answerListCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"

storyBoard上では、cellのidentiferに answerListCellを指定しているので、実際の画面では文字が表示されているのですが、テストだと失敗します。

  • viewController
import UIKit class AnswerListViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "answerListCell", for: indexPath as IndexPath) as! AnswerListCell cell.hogeLabel.text = "hoge" return cell } } class AnswerListCell: UITableViewCell{ @IBOutlet weak var hogeLabel: UILabel! }
  • test
class AnswerListTests: QuickSpec{ override func setUp() { continueAfterFailure = false } override func spec(){ var subject = AnswerListViewController() beforeEach { subject.beginAppearanceTransition(true, animated: false) subject.endAppearanceTransition() } describe("表示"){ it("一行目にLabelが表示されている"){ let indexPath = IndexPath(row: 0, section: 0) subject.tableView.cellForRow(at: indexPath) # この行でエラー発生 expect(cell.hogeLabel!.text).to(equal("hoge")) } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

AnswerListViewControllerのインスタンスはStoryboardから生成しないとダメなんじゃないでしょうか?

投稿2017/06/20 00:41

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kaorimanabe

2017/06/20 01:16 編集

storyboardからインスタンス生成したら行けました。ありがとうございました!助かりました! ・ before var subject = AnswerListViewController() ・ after var storyboard = UIStoryboard(name: "Main", bundle:nil) var subject = storyboard.instantiateViewController(withIdentifier: "answerList") as! AnswerListViewController
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問