Steven Lyons
Steven Lyons

Reputation: 8218

Extension method not being by Xamarin compiler

I'm trying to unit test some code in a Xamarin.iOS project but I'm getting a compiler error I can't resolve. Anyone have advice on how to resolve the error?

In a single solution, I have a Xamarin.iOS project for the non-UI parts of my app and another Xamarin.iOS unit test project. I'm testing out Parse as a backend but want to hide that from most of my app so I added an extension method that will convert my User model to the ParseUser object. When I try to use the extension method in my unit test project, I get the following errror:

/Matchup/MatchupXplatformTests/ParseHelperTests.cs(27,27): Error CS1061: Type MatchupXplatform.Models.User' does not contain a definition forToParseUser' and no extension method ToParseUser' of typeMatchupXplatform.Models.User' could be found. Are you missing an assembly reference? (CS1061) (MatchupXplatformTests)

Here is the code I'm trying to run:

[Test]
public void Pass ()
{
    var user = new User () {
        Username = "[email protected]",
        Email = "[email protected]", 
        Password = "meyoucom", 
        Name = "Me You"
    };

    ParseUser pUser = user.ToParseUser (); // <- Error here

    Assert.Equals (user.Username, pUser.Username);
}

The confusing part is that in the snippet above the code completion in Xamarin Studio resolves the .ToParseUser() extension method, provides accurate information about the method and can find the declaration of the method but the compiler still generates the error.

I've made sure the unit test project references the other project, the unit test file has using statements for the User model and the extension method classes. I've also verified that the code below works in the backend project but not in the unit test project.

Xamarin Studio: Version 4.1.3 (build 66) Xamarin.iOS: Version: 6.3.6.76 (Trial Edition)


UPDATE: As requested, additional code:

MatchupXplatform/Models/User.cs:

using System;
using System.Runtime.Serialization;

namespace MatchupXplatform.Models
{
    public class User
    {
        [DataMember(Name = "username")]
        public string Username { get; set; }

        [DataMember(Name = "email")]
        public string Email { get; set; }

        [DataMember(Name = "password")]
        public string Password { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }
    }
}

MatchupXplatform/ParseHelper.cs:

using System;
using System.Runtime.Serialization;
using System.Reflection;
using System.Collections.Generic;
using Parse;
using MatchupXplatform.Models;

namespace MatchupXplatform
{
    internal static class ParseHelper
    {
        public static ParseUser ToParseUser (this User user) 
        {
            var pUser = new ParseUser ();

            try {
                var pObj = (ParseObject)pUser;
                user.UpdateParseObject (ref pObj);
                return pUser;

            } catch (ParseException pe) {
                Console.WriteLine (pe);
                return null;
            }

        }

        public static User ToUser (this ParseUser pUser)
        {
            return pUser.ToParseObject<User> ();
        }
    }
...
}

MatchupXplatformTests References: Parse.iOS.dll MatchupXplatform monotouch MonoTouch.NUnitLite System System.Core System.Xml

MatchupXplatformTests/ParseHelperTests.cs:

using System;
using NUnit.Framework;
using MatchupXplatform;
using MatchupXplatform.Models;
using Parse;

namespace MatchupXplatformTests
{
    [TestFixture]
    public class ParseHelperTests
    {
        [TestFixtureSetUp]
        public void Setup ()
        {
            ParseStarter.Initialize ();
        }

        [Test]
        public void Pass ()
        {
            var user = new User () {
                Username = "[email protected]",
                Email = "[email protected]", 
                Password = "meyoucom", 
                Name = "Me You"
            };

            ParseUser pUser = user.ToParseUser (); // <- Error here

            Assert.Equals (user.Username, pUser.Username);
        }
    }
}

After struggling with this for a few hours, I ended up just encapsulating this functionality in a service method and then calling that method from the unit test. That worked fine but I'm curious why this generates an error.

Upvotes: 0

Views: 1326

Answers (1)

Greg Munn
Greg Munn

Reputation: 526

It looks like your extension method is internal to your MatchupXplatform assembly and won't be visible to your unit test.

Upvotes: 1

Related Questions