Mocking TIA Openness classes

5 posts, 1 answers
  1. Rami
    Rami avatar
    23 posts
    Member since:
    Sep 2017

    Posted 05 Nov 2018 Link to this post

    Hello,

     

    I'm trying to use JustMock to mock some classes from the TIA Openness https://support.industry.siemens.com/cs/document/108716692/tia-portal-openness%3A-introduction-and-demo-application?dti=0&lc=en-WW library, but I'm having trouble getting the most basic mocking to happen. The TIA Openness classes are referenced through a Siemens.Engineering.dll (which I don't think I'm allowed to link here, sorry)

     

    The Project class is written as following:

    namespace Siemens.Engineering {
        [DebuggerNonUserCode]
        public sealed class Project : IEngineeringObject, IEngineeringCompositionOrObject, IEngineeringInstance, ITransactionSupport, IMasterCopyTarget, IInternalObjectAccess, IInternalInstanceAccess, IInternalBaseAccess, IEngineeringServiceProvider, IServiceProvider, IEquatable<object> {
            public HardwareUtilityComposition HwUtilities { get; }
            public FileInfo Path { get; }
            public string Name { get; }
            public string LastModifiedBy { get; }
            public DateTime LastModified { get; }
            public bool IsModified { get; }
            public string Family { get; }
            public DateTime CreationTime { get; }
            public string Copyright { get; }
            public string Author { get; }
            public UsedProductComposition UsedProducts { get; }
            public DeviceSystemGroup UngroupedDevicesGroup { get; }
            public SubnetComposition Subnets { get; }
            public ProjectLibrary ProjectLibrary { get; }
            public IEngineeringObject Parent { get; }
            public LanguageSettings LanguageSettings { get; }
            public string Version { get; }
            public HistoryEntryComposition HistoryEntries { get; }
            public MultiLingualGraphicComposition Graphics { get; }
            public DeviceComposition Devices { get; }
            public DeviceUserGroupComposition DeviceGroups { get; }
            public MultilingualText Comment { get; }
            public long Size { get; }
     
            public void Close();
            public override bool Equals(object obj);
            public void ExportProjectTexts(FileInfo path, CultureInfo sourceLanguage, CultureInfo targetLanguage);
            public IList<object> GetAttributes(IEnumerable<string> names);
            public override int GetHashCode();
            public T GetService<T>() where T : class, IEngineeringService;
            public ProjectTextResult ImportProjectTexts(FileInfo path, bool updateSourceLanguage);
            public void Save();
            public void SetAttributes(IEnumerable<KeyValuePair<string, object>> attributes);
            public void ShowHwEditor(View view);
            public override string ToString();
        }
    }

     

    And I'm trying to get the mocking started just by mocking the getter on Name as follows:

    [TestMethod]
            public void Simplest() {
                var ret = Mock.Create<Project>(Behavior.Strict);
                Mock.Arrange(() => ret.Name).Returns("Mocked project");
                Assert.AreEqual("Mocked project", ret.Name);
            }

     

    But everytime the test fails with the same error:

    Message: Test method UnitTestProject.SequenceTests.Simplest threw exception:
    System.IO.FileNotFoundException: Could not load file or assembly 'Siemens.Engineering.Contract, Version=1500.0.2601.1, Culture=neutral, PublicKeyToken=37a18b206f7724a6' or one of its dependencies. The system cannot find the file specified.

     

    The stack trace is just

    Project.get_name()

    SequenceTests.Simplest()

     

    The actual usage of TIA Openness requires, for example, for the user to be in a specific Windows user group etc., but I thought I could just intercept all calls to the actual TIA Openness and mock everything away? I was under the impression (and that is what happens when I test JustMock on a class I wrote) that I could overwrite the whole getter of Name (and in the future other, more relevant properties)? But it doesn't seem that is the case. If someone could point me in the right direction, it would be appreciated.

  2. Rami
    Rami avatar
    23 posts
    Member since:
    Sep 2017

    Posted 06 Nov 2018 in reply to Rami Link to this post

    So, I found DebugView today. But that doesn't seem to help me. When I run it on a class library I made for testing, the FullTrace shows everything going really well:

      Resolving repository with unresolved context behavior CreateNewContextualOrLocal
    Created mocks repository #1 for Void LibraryMockTest():
    Interception of type SealedClassLibrary.Library is now on:
        Resolving repository with unresolved context behavior CreateNewContextualOrLocal
    Intercepted profiler call: SealedClassLibrary.Library.get_Name():
        Stack trace:
                at SealedClassLibrary.Library.get_Name()
                at UnitTestProject.JustMockTest.LibraryMockTest() in C:\Temp\SealedMockTest\UnitTestProject\UnitTest1.cs:line 35
                at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
                at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
                at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
                at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions.MethodInfoExtensions.InvokeAsSynchronousTask(MethodInfo methodInfo, Object classInstance, Object[] parameters)
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodInfo.<>c__DisplayClass39_0.<ExecuteInternal>b__0()
                at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodInfo.ExecuteInternal(Object[] arguments)
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodInfo.Invoke(Object[] arguments)
                at Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.Execute(ITestMethod testMethod)
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodRunner.RunTestMethod()
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodRunner.Execute()
                at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner.RunSingleTest(TestMethod testMethod, IDictionary`2 testRunParameters)
         
        Handling dispatch in repo #1 servicing Void LibraryMockTest()
        Inspect arrangements on System.String get_Name() on SealedClassLibrary.Library
            this: Match -> "ByRef SealedClassLibrary.Library" is "ByRef SealedClassLibrary.Library"
            Found candidate arrangement (id=0) () => m.Name
        Chosen arrangement (id=0) () => m.Name
        Calls so far: 1
        Returning value 'Steve'

     

    We see the call is intercepted and my arranged property value 'Steve' is returned and the Assert. passes. But when I look at the same on the TIA Project arrangement,it seems it is trying to Arrange, but doesn't intercept the call. The CurrentState after the Arrange stays the same through out the test method:

    Elevated mocking: enabled
    Arrangements and expectations:
        Arrangement (id=0) () => ret.Name:

    Invocations:
        --none--

     

    And the FullTrace stays the same all through the test method:

        Resolving repository with unresolved context behavior CreateNewContextualOrLocal
    Created mocks repository #1 for Void Simplest():
    Interception of type Siemens.Engineering.Project is now on:
        Resolving repository with unresolved context behavior CreateNewContextualOrLocal

  3. Answer
    Ivo
    Admin
    Ivo avatar
    22 posts

    Posted 07 Nov 2018 Link to this post

    Hello Rami,

    The issue is more likely to be related to the specific configuration required for detecting TIA Openness installation path. I took a look at "Configuration file/AssemblyResolve" topic from this document and most probably this is the case. In addition you also might need to run the tests with dedicated rights described in the mentioned document too.

    I hope that it helps.


    Regards,
    Ivo
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Rami
    Rami avatar
    23 posts
    Member since:
    Sep 2017

    Posted 08 Nov 2018 in reply to Ivo Link to this post

    Hello Ivo,

     

    I had some success with the AssemblyResolve, but in the end it did not work out. I slightly rearranged some classes to avoid calling the TIA Openness stuff in my unit tests alltogether.

     

    Regards, Rami

  5. Ivo
    Admin
    Ivo avatar
    22 posts

    Posted 13 Nov 2018 Link to this post

    Hello Rami,

    I am glad to hear that you managed to resolve your issue. Please feel free to write us back in case of any additional questions.


    Regards,
    Ivo
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top