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

  • SEARCH
  • Home
  • 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 626325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:24:25+00:00 2026-05-13T19:24:25+00:00

I’m brand new to unit testing and mocking and still wet behind the ears.

  • 0

I’m brand new to unit testing and mocking and still wet behind the ears. I’m using the Moq framework and I need to mock a collection such that it yields a single member with a value I supply.

The collection class in question is a System.Configuration.SettingsPropertyCollection, which contains SettingsProperty objects.

In turn, the SettingsProperty has an Attributes property that returns a SettingsAttributeDictionary.

I need my collection to yield a single SettingsProperty, which has a single custom attribute (derived from System.Attribute) in its Attributes.SettingsAttributeDictionary.

I’m really struggling to get my head around this, but so far to no avail. I’ve tried sticking out my tongue and pulling funny faces at it, but nothing works.

Here’s the code I’ve tried so far, an exception is thrown at the point commented in the code and so of course the test always fails.

    [TestMethod]
    public void GetPropertySettings_Should_Return_Default_Values_When_Device_Not_Registered()
        {
        const string deviceName = "My.UnitTest";
        const string deviceType = "Switch";
        var deviceId = String.Format("{0}.{1}", deviceName, deviceType);
        Mock<IProfile> mockProfile = new Mock<IProfile>();
        Mock<SettingsContext> mockSettingsContext = new Mock<SettingsContext>();
        // Construct a SettingsPropertyCollection populated with a single property.
        // The single property will have a fixed name and default value, and will also have a single
        // attribute, giving teh ASCOM DeviceId.
        var deviceAttribute = new ASCOM.DeviceIdAttribute(deviceId);
        var attributes = new SettingsAttributeDictionary();
        attributes.Add(typeof(DeviceIdAttribute), deviceAttribute);
        var settingsProperty = new SettingsProperty(SettingName, typeof(string), null, false, SettingDefaultValue, SettingsSerializeAs.String, attributes, true, true);
        var propertyCollection = new SettingsPropertyCollection();
        propertyCollection.Add(settingsProperty);

        // Now comes the interesting part where we call our IProfile - this is where we really need Moq.
        // Expectations:
        //  - mockProfile must have it's DeviceType set.
        //  - mockProfile's device type (captured in setDeviceType) must match deviceType.
        //  - The returned SettingsPropertyValueCollection must not be empty.
        //  - The returned SettingsPropertyValueCollection must have exactly one entry.
        //  - The entry must match the value of SettingDefaultValue. 

        // Expectation: IProfile must have its DeviceType set.  We capture the value into setDeviceType.
        var setDeviceType = String.Empty;
        mockProfile.SetupSet(x => x.DeviceType).Callback(y => setDeviceType = y);

        // Finally, it is time to call the method we want to test
        var settingsProvider = new SettingsProvider(mockProfile.Object);

        // THE NEXT LINE THROWS AN EXCEPTION
        // IF I TRY TO STEP INTO IT, IT NEVER RETURNS AND THE TEST RUN JUST ENDS.
        var result = settingsProvider.GetPropertyValues(mockSettingsContext.Object, propertyCollection);

        // Now lets verify that everything went as expected
        // First, let's test that the parsing of DeviceId was as expected: IProvider.DeviceType was set to the expected value
        Assert.AreEqual(deviceType, setDeviceType);
        // Then let's test that the methods of IProvider that we mocked were called
        mockProfile.VerifyAll();

        // With this done, let's turn to the output of the method
        // Firstly, we test that the resulting collection contains exactly one item of the type SettingsPropertyValue
        Assert.IsTrue(result.Count > 0);
        Assert.AreEqual(1, result.Count);
        Assert.IsTrue(result.OfType<SettingsPropertyValue>().Count() > 0);

        // Then let's inspect the contained SettingsProviderValue further
        var settingsPropertyValue = result.OfType<SettingsPropertyValue>().First();

        // First IsDirty flag must never be set
        Assert.IsFalse(settingsPropertyValue.IsDirty);

        // The PropertyValue must be the default value we passed in
        Assert.AreEqual(SettingDefaultValue, settingsPropertyValue.PropertyValue);
        }

The exception that gets thrown (as reported by the test runner) is:

Test method ASCOM.Platform.Test.SettingsProviderTest.GetPropertySettings_Should_Return_Default_Values_When_Device_Not_Registered threw exception: System.ArgumentException: The type System.Configuration.SettingsContext implements ISerializable, but failed to provide a deserialization constructor.

  • 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-13T19:24:25+00:00Added an answer on May 13, 2026 at 7:24 pm

    I believe you need to set up the Attributes property of your mock SettingsProperty to return your mock SettingsAttributeDictionary and then set up the indexer of your mock SettingsAttributeDictionary to return your desired value, e.g.

    mockItem.SetupGet(x => x.Attributes).Returns(mockAttributes);
    
    mockAttributes.SetupGet(x => x[It.IsAny<System.Type>()])
                  .Returns(deviceAttribute);
    

    Edit

    The exception is being thrown in the Castle DynamicProxy class which is used by Moq. I believe this is to do with how Moq mocks objects which are serializable. If castle can’t find a non-public constructor on a serializable object with a signature of (SerializationInfo, StreamingContext) then it throws this exception. What you could do, is change the GetPropertyValues method of your custom SettingsProvider so that it accepts a Hashtable instead of a SettingsContext, and provide a mock Hashtable to the method call instead of a mock SettingsContext. Hashtable has the required constructor so perhaps this will work. There is no real advantage to insisting on a parameter of type SettingsContext rather than Hashtable since SettingsContext is only trivially derives from Hashtable, i.e. it adds no members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.