# Chapter 7: Unit Testing - occasional crash

**URL:** <https://forums.kodeco.com/t/chapter-7-unit-testing-occasional-crash/26589>\
**Category:** Core Data by Tutorials\
**Created:** [February 24, 2017, 8:17pm UTC](https://forums.kodeco.com/t/chapter-7-unit-testing-occasional-crash/26589 "2017-02-24T20:17:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpashch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bpashch/32/106789_2.png) [@bpashch](https://forums.kodeco.com/u/bpashch)\
**Post date:** [February 24, 2017, 8:17pm UTC](https://forums.kodeco.com/t/chapter-7-unit-testing-occasional-crash/26589/1 "2017-02-24T20:17:18Z")

</div>

I’m facing this issue on the screenshot every second time or so. Happens on chapter 7’s starter project with a few added tests.

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/c/3/c3426e54790567a50055bc7e1d733520f93ab7fa.png)

Here’re some tips that this relates to wrong module name being used in testing environment: [ios - Swift Unit Test EXC\_BAD\_ACCESS(code=1, address=0x8) - Stack Overflow](http://stackoverflow.com/a/30468417/1675788)  
I’ve converted `SO` answer `swift 3` , fed it to `NSPersistentContainer`’s `init` and it didn’t help:

```
lazy var managedObjectModel: NSManagedObjectModel =
    {
      let modelURL = Bundle.main.url(forResource: self.modelName, withExtension: "momd")
      let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL!)
      
      let environment = ProcessInfo.processInfo.environment
      
      let isTest:Bool
      if let count = environment["XCTestConfigurationFilePath"]?.characters.count, count > 0 { isTest = true }
      else { isTest = false }
      
      let moduleName = (isTest) ? "CampgroundManagerTests" : "CampgroundManager"
      
      // Create a new managed object model with updated entity class names
      var newEntities = [] as [NSEntityDescription]
      for entity in managedObjectModel!.entities
      {
        let newEntity = entity.copy() as! NSEntityDescription
        newEntity.managedObjectClassName = "\(moduleName).\(entity.name)"
        newEntities.append(newEntity)
      }
      let newManagedObjectModel = NSManagedObjectModel()
      newManagedObjectModel.entities = newEntities
      
      return newManagedObjectModel
  }()

```

I also see this in the top of my console output:

> CoreData: warning: Unable to load class named ‘CampgroundManagerTests.Optional(“CampSite”)’ for entity ‘CampSite’. Class not found, using default NSManagedObject instead.

What’s the simplest way to fix this error?  
Thanks 🙂

---

<div class="post-metadata">

**Author:** ![tetanuss](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tetanuss/32/81494_2.png) [@tetanuss](https://forums.kodeco.com/u/tetanuss)\
**Post date:** [February 26, 2017, 7:19am UTC](https://forums.kodeco.com/t/chapter-7-unit-testing-occasional-crash/26589/2 "2017-02-26T07:19:56Z")

</div>

Sorry, that code from Stackoverflow looks a bit weird to me… What version of the CoreData Book do you have. I had no Problem with the Tests in version 3.1 - all was straight forward… (in this Part)

---

<div class="post-metadata">

**Author:** ![bpashch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bpashch/32/106789_2.png) [@bpashch](https://forums.kodeco.com/u/bpashch)\
**Post date:** [February 26, 2017, 11:02am UTC](https://forums.kodeco.com/t/chapter-7-unit-testing-occasional-crash/26589/3 "2017-02-26T11:02:48Z")

</div>

Looks like it basically prepend managedObjectClassName with module name (that was the idea of the SO post). I have Core\_Data\_by\_Tutorials\_v3.1.pdf
