magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.79k stars 1.79k forks source link

No shorthand in unit test #363

Closed ignazioc closed 10 years ago

ignazioc commented 11 years ago

I use magicalRecord in my project and i use it even inside UnitTest. But inside unit test shorthands are not recognized.

I use cocoapod to manage dipendencies.

tonyarnold commented 11 years ago

Hi @ignazioc — have you defined the MR_SHORTHAND flag for your unit test target?

ignazioc commented 11 years ago

in test target? no i defined MR_SHORTHAND inside a test class.

here some others detail:

platform :ios, '5.0' pod 'CocoaLumberjack', '~> 1.6' pod 'ECSlidingViewController', '~> 0.9.0' pod 'AFNetworking', '1.0' pod 'MagicalRecord', '2.0.8' pod 'MBProgressHUD' pod 'OCHamcrest' pod 'OCMockito'

  • In pods library target i added -all-load and -Objc for "Other linker Flags"
  • Pods-prefix.pch there are this lines

define MR_SHORTHAND

define MR_ENABLE_ACTIVE_RECORD_LOGGING 0

import "CoreData+MagicalRecord.h"

  • this is the test case

define MR_SHORTHAND

import "Customer_category+CustomCode.h"

import <SenTestingKit/SenTestingKit.h>

@interface CustomerCategoryTest : SenTestCase @end

@implementation CustomerCategoryTest { Customer_category *sut; }

  • (void)setUp { [super setUp]; [MagicalRecord setupCoreDataStackWithInMemoryStore]; }
  • (void)tearDown { [Customer_category truncateAll]; <- method not find! [super tearDown]; }

thank you. ignazioc

On 5, Jan, 2013, at 1:48 PM, Tony Arnold notifications@github.com wrote:

Hi @ignazioc — have you defined the MR_SHORTHAND flag for your unit test target?

— Reply to this email directly or view it on GitHub.

tonyarnold commented 11 years ago

You need to define MR_SHORTHAND before the MagicalRecord header is imported. It has no effect if you define it in your individual test class implementations, and aren't importing MagicalRecord there.

Where are you importing CoreData+MagicalRecord.h for your test target? Your PCH? Define MR_SHORTHAND above the MagicalRecord header import and it should work fine.

ignazioc commented 11 years ago

i use my project pch, and yes it is declared before CoreData+MagialRecord.h This the piece of PCH file

#define MR_SHORTHAND
#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
#import "CoreData+MagicalRecord.h"

the same files works on unit test target…so i don't understand why doesnt'recognize shorthand. maybe some configuration of cocoa pods?

thanks

On 5, Jan, 2013, at 11:00 PM, Tony Arnold notifications@github.com wrote:

You need to define MR_SHORTHAND before the MagicalRecord header is imported. It has no effect if you define it in your individual test class implementations, and aren't importing MagicalRecord there.

Where are you importing CoreData+MagicalRecord.h for your test target? Your PCH? Define MR_SHORTHAND above the MagicalRecord header import and it should work fine.

— Reply to this email directly or view it on GitHub.

casademora commented 11 years ago

Make sure you do the same in the pch file for your test target. This gets me all the time.

Saul Mora @casademora

On Jan 5, 2013, at 3:09 PM, Ignazio notifications@github.com wrote:

i use my project pch, and yes it is declared before CoreData+MagialRecord.h This the piece of PCH file

define MR_SHORTHAND

define MR_ENABLE_ACTIVE_RECORD_LOGGING 0

import "CoreData+MagicalRecord.h"

the same files works on unit test target…so i don't understand why doesnt'recognize shorthand. maybe some configuration of cocoa pods?

thanks

On 5, Jan, 2013, at 11:00 PM, Tony Arnold notifications@github.com wrote:

You need to define MR_SHORTHAND before the MagicalRecord header is imported. It has no effect if you define it in your individual test class implementations, and aren't importing MagicalRecord there.

Where are you importing CoreData+MagicalRecord.h for your test target? Your PCH? Define MR_SHORTHAND above the MagicalRecord header import and it should work fine.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/magicalpanda/MagicalRecord/issues/363#issuecomment-11920745.

ignazioc commented 11 years ago

i'm sorry…i have the same PCH file (it's Xcode default) for main target and test target. Even if i try to set a brand new PCH file, problem is the same.

...i feel so stupid ...

On 5, Jan, 2013, at 11:11 PM, Saul Mora notifications@github.com wrote:

Make sure you do the same in the pch file for your test target. This gets me all the time.

Saul Mora @casademora

On Jan 5, 2013, at 3:09 PM, Ignazio notifications@github.com wrote:

i use my project pch, and yes it is declared before CoreData+MagialRecord.h This the piece of PCH file

define MR_SHORTHAND

define MR_ENABLE_ACTIVE_RECORD_LOGGING 0

import "CoreData+MagicalRecord.h"

the same files works on unit test target…so i don't understand why doesnt'recognize shorthand. maybe some configuration of cocoa pods?

thanks

On 5, Jan, 2013, at 11:00 PM, Tony Arnold notifications@github.com wrote:

You need to define MR_SHORTHAND before the MagicalRecord header is imported. It has no effect if you define it in your individual test class implementations, and aren't importing MagicalRecord there.

Where are you importing CoreData+MagicalRecord.h for your test target? Your PCH? Define MR_SHORTHAND above the MagicalRecord header import and it should work fine.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/magicalpanda/MagicalRecord/issues/363#issuecomment-11920745. — Reply to this email directly or view it on GitHub.

tonyarnold commented 11 years ago

You're not stupid at all, @ignazioc — if the feature is causing this much trouble, we need to spend some time writing better documentation about setting it up. I haven't been using the shorthand methods, so I haven't hit this problem.

Can you try adding MR_SHORTHAND to the Preprocessor Macros field under the Build Settings for your unit test target? That should have the same effect.

ignazioc commented 11 years ago

we have the same error. Tomorrow i will try to create a new project, without cocoa pods, to see the difference.

thanks

On 6, Jan, 2013, at 12:23 AM, Tony Arnold notifications@github.com wrote:

You're not stupid at all, @ignazioc — if the feature is causing this much trouble, we need to spend some time writing better documentation about setting it up. I haven't been using the shorthand methods, so I haven't hit this problem.

Can you try adding MR_SHORTHAND to the Preprocessor Macros field under the Build Settings for your unit test target? That should have the same effect.

— Reply to this email directly or view it on GitHub.

ignazioc commented 11 years ago

I have another clue. It isn't a fault of cocoapods or library or something related. I have created an empty project, added manually magical source files and defined everityhing about magicalrecord inside pch file. In the maintarget shorthands are visible, but in test target they cause a runtime error "method not found" (a compile time i have no error).

You can find this test project here: https://github.com/netfocus/AFN.git

thanks a lot.

On 6, Jan, 2013, at 12:59 AM, Ignazio Calò ignazioc@gmail.com wrote:

we have the same error. Tomorrow i will try to create a new project, without cocoa pods, to see the difference.

thanks

On 6, Jan, 2013, at 12:23 AM, Tony Arnold notifications@github.com wrote:

You're not stupid at all, @ignazioc — if the feature is causing this much trouble, we need to spend some time writing better documentation about setting it up. I haven't been using the shorthand methods, so I haven't hit this problem.

Can you try adding MR_SHORTHAND to the Preprocessor Macros field under the Build Settings for your unit test target? That should have the same effect.

— Reply to this email directly or view it on GitHub.

tonyarnold commented 11 years ago

Thanks for the example — I can confirm that this is happening for me with your project.

Here's why: MagicalRecord requires CoreData — your PCH does not import the CoreData header before loading MagicalRecord's header. Add the following to your PCH, below the Foundation header import, and it should all start working (it did for me with your sample project):

#import <CoreData/CoreData.h>

Post back here and let me know how it goes.

ignazioc commented 11 years ago

unit test actually works with coredata , everything works if i use MR_ methods, only the shorthand cause this error at runtime

This is the actually pch file

import

ifndef __IPHONE_4_0

warning "This project uses features only available in iOS SDK 4.0 and later."

endif

ifdef OBJC

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#define MR_SHORTHAND
#import "CoreData+MagicalRecord.h"

endif

and this is the error

2013-01-06 14:05:03.039 AFN[54159:c07] +[Person createEntity]: unrecognized selector sent to class 0x75445f8

can you push back your project version?

thanks.

On 6, Jan, 2013, at 1:42 PM, Tony Arnold notifications@github.com wrote:

Thanks for the example — I can confirm that this is happening for me with your project.

Here's why: MagicalRecord requires CoreData — your PCH does not import the CoreData header before loading MagicalRecord's header. Add the following to your PCH, below the Foundation header import, and it should all start working (it did for me with your sample project):

import <CoreData/CoreData.h>

Post back here and let me know how it goes.

— Reply to this email directly or view it on GitHub.

squarefrog commented 11 years ago

I'm experiencing the same issue using MagicalRecord and Cocoapods. If I use MR_createEntity it works fine, completely ignoring the shared .pch file.

squarefrog commented 11 years ago

It seems odd that it only affects test targets. I might just use the MR_ prefix in my test. Thanks for the quick response.

peterfriese commented 11 years ago

I struggled with this problem as well. I use CocoaPods to manage my dependencies. The test target did not contain Core Data, which resulted in messages like unrecognized selector send to instance.... I finally found out that adding CoreData to the test target fixes the problem.

squarefrog commented 11 years ago

My test target already contains CoreData.framework. Tests work great when using the MR prefix. It's not a big deal, as using the MR prefix fixes the issues. It's just something to remember when constructing tests.

jeremymedford commented 11 years ago

I am experiencing this same issue but inside of my Core Data entity class under test as well as my unit test files. Both the main target and test target .pch files correctly order the #define MRSHORTHAND followed by the #import "CoreData+MagicalRecord.h" statements. I verified that CoreData is added to the test target. Using 'MR' removes the issue. I will continue to investigate.

brewpoo commented 11 years ago

I've been having the same issue here. Using MR_ prefix in tests does not help if class under test is using the shorthand methods. This is the case for me.

Removing the Core Data entity class implementation files from the unit test build dependency (uncheck the unit test target for them) fixes the issue entirely but then there is no coverage reporting for the classes under test.

jkoepcke commented 11 years ago

I can see the same issue, I don't have a solution yet. Investigation on this should continue.

ketzusaka commented 11 years ago

This appears to only occur when the unit tests are setup for application tests. If "test host" and "bundle loader" build settings are defined I see this error. If I'm just doing logical tests, everything runs fine.

siuying commented 10 years ago

Yes, after remove default test host and bundle loader and it works.

ketzusaka commented 10 years ago

The downside is that you then can't test anything that requires the UIApplication, which most view controllers and more integration-like tests need.

tonyarnold commented 10 years ago

I don’t think there’s going to be an easy answer to this one, guys. Currently, MagicalRecord uses method swizzling to implement the shorthand method names at runtime — there’s something in the way unit test bundles are executed that’s causing this swizzling to fail.

I’ll have a look at how other projects approach this, but for now I’m pushing a fix for this issue off to MagicalRecord 3.x.

Thanks for being patient.

colasbd commented 10 years ago

I have the same problem, but not in the Test, in the normal target.

Here the .pch of the project.

#ifdef __OBJC__
    #import <Cocoa/Cocoa.h>
    /*
     In order not to always prefix the methods...
     */
    #define MR_SHORTHAND
    /*
     To disable the logging of MR.
     */
    #define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
    #import "CoreData+MagicalRecord.h"
#endif

I even tried to modify Pods-MagicalRecord-prefix.pch to

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif

#import "Pods-environment.h"

#import <CoreData/CoreData.h>
#define MR_SHORTHAND
#import "CoreData+MagicalRecord.h"

but I still have error +[NSManagedObjectContext newMainQueueContext]: unrecognized selector sent to class 0x7fff7d732b70

siuying commented 10 years ago

@colasjojo Have you tried to include the "MagicalRecord/Shorthand" spec?

https://github.com/CocoaPods/Specs/blob/master/MagicalRecord/2.2/MagicalRecord.podspec

colasbd commented 10 years ago

@siuying I don't understand what you mean. My Pod is

platform :osx, '10.7'
xcodeproj 'TestDocumentBasedCoreDataApp.xcodeproj'
pod 'MagicalRecord'

Shall I add something to it?

siuying commented 10 years ago

@colasjojo the podspec no longer include shorthand by default. You can include it this way:

pod 'MagicalRecord/Shorthand'

For details on this change, please refer to discussion here: https://github.com/CocoaPods/Specs/commit/9e7d7aa808ed19c4e6ea1e9647cd2a7668560886#commitcomment-5290835

colasbd commented 10 years ago

Thanks. Do I have to include both MagicalRecord and MagicalRecord/Shorthand or just the latter?

Anyway, since I saw what they say, I changed my mind and may not want to use MR_SHORTHAND...

siuying commented 10 years ago

Just the alter. Yeah I have since change my code base and remove shorthands too :+1: