Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3598400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:14:15+00:00 2026-05-18T20:14:15+00:00

I wonder is anyone have tryed using Grensesnitt for unit-testing of classes that all

  • 0

I wonder is anyone have tryed using Grensesnitt for unit-testing of classes that all follow the same interface. I have a problem with classes that don’t have parameterless constructors. I know there is GrensesnittObjectLocator but I can’t figure out, how to use it.

Please advice, how to test these classes that don’t have parameterless constructors with grensesnitt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:14:15+00:00Added an answer on May 18, 2026 at 8:14 pm

    I didn’t manage to make this working out of the box. I had to tweak it a little. So inside the GrensesnittObjectLocator.GetHandler method instead of:

    public static Func<object> GetHandler(Type T)
    {
        Func<object> handler;
        if (handleAll != null) return () => { return handleAll(T); };
        if (map.TryGetValue(T, out handler))
        {
            return (Func<object>)handler;
        }
        return () => { return TryReflections(T); };
    }   
    

    I modified it to:

    public static Func<object> GetHandler(Type T)
    {
        return () =>
        {
            Func<object> handler;
            if (handleAll != null) return handleAll(T);
            if (map.TryGetValue(T, out handler))
            {
                return handler();
            }
            return TryReflections(T);
        };
    }       
    

    With this modification in place I wrote the following exmaple:

    public interface IFoo
    {
        int Add(int a, int b);
    }
    
    public class Foo : IFoo
    {
        private readonly string _foo;
        public Foo(string foo)
        {
            _foo = foo;
        }
    
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    You can see how the Foo class doesn’t have a default constructor. So now we can have this test:

    [InterfaceSpecification]
    public class IFooTests : AppliesToAll<IFoo>
    {
        [Test]
        public void can_add_two_numbers()
        {
            Assert.AreEqual(5, subject.Add(2, 3));
        }
    }
    

    And in order to indicate to grensesnitt how to instantiate Foo simply add the following class to your test assembly (the same assembly that contains the previous unit test):

    [SetUpFixture]
    public class Config
    {
        [SetUp]
        public void SetUp()
        {
            // indicate to Grensesnitt that the Foo class
            // doesn't have a default constructor and that 
            // it is up to you to provide an instance
            GrensesnittObjectLocator.Register<Foo>(() =>
            {
                return new Foo("abc");
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder is anyone have tryed using Grensesnitt for unit-testing of classes that all
I wonder if anyone else is facing the same issue. I have a UITextView
I wonder if anyone has encountered the same problem and how they solve it.
I wonder if anyone has an idea how you have to transform an image
I wonder if anyone can help - I have this error showing recently when
I have a bit of a pickle here and wonder if anyone can give
I wonder if anyone tried using Kaazing for client push with Silverlight? How did
I wonder if anyone could help me with a problem I've been having. I
I wonder if anyone can help with a jQuery problem I am having. I
We have been using the following js/regex to find and replace all non-alphanumeric characters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.