ios - swift - Unit test CoreData (+ MagicalRecord) model triggers EXC_BAD_ACCESS -


i need unit test (xctest) of methods include reference coredata models.

the following line execute correctly :

var airport: anyobject! = airport.mr_createentity()  (lldb) po airport <airport: 0x7fcf54216940> (entity: airport; id: 0x7fcf54216a20 <x-coredata:///airport/t1d3d08da-70f9-4da0-9487-bd6047ee93692> ; data: {     open = nil;     shortname = nil;     visible = nil; }) 

whereas following line triggers exc_bad_access :

var airport2: airport = airport.mr_createentity() as! airport  (lldb) po airport2 error: execution interrupted, reason: exc_bad_access (code=1, address=0x0). process has been returned state before expression evaluation. 

no sign of error principal target. configuration : model objects in both targets, class prefixed @objc(mymodel), no namespace in class' models in xcdatamodel

any idea what's going on here ?

right, have gotten bottom of , not pretty. there radar issue appears bug swift compiler not recognizing managedobject casting in test targets. add voice noise

starting off entity defined follows:

@objc(member) class member: nsmanagedobject {         @nsmanaged var name: string } 

i wrote simple test class in create mo in 3 different ways:

the first 2 failed:

let context = nsmanagedobjectcontext.mr_defaultcontext()  func testmagicalrecordcreation() {     let m = member.mr_createincontext(context) as? member     xctassertnotnil(m, "failed create object")//fails }  func testentitydescriptionclasscreation() {     let m2 = nsentitydescription.insertnewobjectforentityforname("member", inmanagedobjectcontext: context) as? member     xctassertnotnil(m2, "failed create object")//fails } 

and success

func testmanualmocreation() {     let ent = nsentitydescription.entityforname("member", inmanagedobjectcontext: context)!     let m3 = member(entity: ent, insertintomanagedobjectcontext: context)     xctassertnotnil(m3, "failed create object") } 

this means right have 2 options. write tests in objective-c; or create utility method insert test objects context using means showed above.

theres nice post behaviour here

i ended using nsmanagedobjectcontext extension used explicitly in swift tests:

extension nsmanagedobjectcontext {     func inserttestentity<t: nsmanagedobject>(entity: t.type) -> t {         let entitydescription = nsentitydescription.entityforname(nsstringfromclass(t.self), inmanagedobjectcontext: self)!         return t(entity: entitydescription, insertintomanagedobjectcontext: self)     } } 

and used this:

func testconveniencecreation() {     let m4 = context.inserttestentity(member)     xctassertnotnil(m4, "failed create object") } 

more reading kind of approach here


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -