Unittest for a PCL - LINQ method fails

We like to use TTD and with Xamarin we use NUnit as testframework. In our project we have a PCL and a monodroid/montouch project. We have also iOS and Android integrationtest-projects (with NUnit but with the special testrunner).

My problem is that real unittests (the fast ones executed NOT in the simulator) we use for TTD don't work with the PCL (and there it's most important) as soon as we use LINQ-extension-methods like for example


The exception we get is always:

could not load file or assembly system.core

If we don't use linq methods, everything works fine... Maybe as info: in the integrationtest-projects we can execute this tests without problem, with our without LINQ extensions.

The PCL looks like that:


And the unittest-project is a Xamarin-Studio "NUnit Library Project" (we also tried a lot of other project-types without success).

I spent a lot of time for googling this problem. According to this post, it should work this way: http://stackoverflow.com/questions/18175191/how-to-run-unit-tests-for-xamarin-library-projects-ios-and-android

Can anybody help me with this? Or is it not possible?

FYI: I asked the same question on SO (http://stackoverflow.com/questions/23277298/unittest-for-a-pcl-linq-method-fails). See poupou's questions for more information...
A few upvotes, but unfortunately no answer. So I hope you experts here know more!


  • PeterDavisPeterDavis USMember ✭✭✭
    edited May 2014

    Where is the Enumerable.Any() call? Is it happening in the PCL or in the unit test project? Do you have any references besides the ".NET Portable Subset"? I just checked my unit tests (we use MS Test, but that shouldn't make a difference) and I'm hitting this line in my PCL:

    if (!db.Table<SQLiteMaster>().Any(p => p.Type == "table" && p.Name == "userdetails"))
        return false;

    And that works just fine. So I'm wondering if you've got something else going on. You might try creating a new project from scratch and then copy your source over to it to see if things somehow got corrupted.

    My .csproj has more or less the same stuff you have up there. Same ProjectTypeGuids, same TargetFrameworkVersion, so I think that's all fine.

  • UrsinBrunnerUrsinBrunner CHMember ✭✭

    Hi Peter

    The call is happening in the PCL. When I call .Any() in the Test-Project, there is no problem.

    Actually we have a lot more references in the PCL, you can see them here: https://dl.dropboxusercontent.com/u/96491913/Screen Shot 2014-04-26 at 8.57.34 AM.png

    I will try creating a new project and re-add the references step by step, thats a good idea.

  • PeterDavisPeterDavis USMember ✭✭✭
    edited May 2014

    You have what looks like a few android specific libraries (Restsharp.monodroid.dll, Zxing.monodroid.dll) in there. You can't do that in a PCL. It needs to be portable which means it can't have any Android specific or iOS specific references. Those will almost surely cause you problems.

    Anything Android specific needs to exist only in your Android application. Likewise, anything iOS specific must exist only in your iOS app. If you need your PCL to call some of that code, you're going to need to abstract it out.

    See this post for one approach to handling this.

  • UrsinBrunnerUrsinBrunner CHMember ✭✭

    Hi peter, sorry for not clarify this point... the platform-specific references are included this way:

    <Reference Include="ZXing.Net.Mobile" Condition="'$(Platform)' == 'Android'">

    And because they are source code compatible, there is no problem with this (btw we have some parts we abstract exact in the same way you describe in the link).

    The app is already in the appstore/playstore and working fine, my problem is only how to unit test it.

Sign In or Register to comment.