chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Can not mock chained statements with 'struct' objects. bar.Foo() returns System.Void


Hi, is there any workaround (except changing function to return something) to mock void functions in 4.01 ?
asked by Gmoorick (2.3k points)

4 Answers

0 votes
Hi
There shouldn't be any problem in doing this.
Can you please post your test code?
answered by ohad (35.4k points)
0 votes
Only some of my void functions got these errors.
first one
        [Test]
        public void ZipFiles_SomeFilesRegistered_ZipFilesMoveToOutputDirAndDeleteSources()
        {
            string outputPath = "d:\temp\myoutput.zip";
            FileManager fileManager = new FileManager();
            using (RecordExpectations rec = RecorderManager.StartRecording())
            {
                rec.DefaultBehavior.CheckArguments();
                FileRoutines.FileExists("file1");
                rec.Return(true);
                FileRoutines.FileExists("file2");
                rec.Return(true);
                
                ZipFile zipFile = ZipFile.Create("myoutput.zip");
                zipFile.BeginUpdate();

/*TypeMock.TypeMockException: 
*** Can not mock chained statements with 'struct' objects. ZipFile.BeginUpdate() returns System.Void

в TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5) 
в TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Object p1) 
в ICSharpCode.SharpZipLib.Zip.ZipFile.Add(String fileName) 
в BiK.UnitTests.FileManagerTests.ZipFiles_SomeFilesRegistered_ZipFilesMoveToOutputDirAndDeleteSources() в D:workik  
*/
                zipFile.Add("file1");  
                zipFile.Add("file2");
                zipFile.CommitUpdate();
                zipFile.Close();
                zipFile.TestArchive(true);
                rec.Return(true);
                FileRoutines.MoveFileTo("myoutput.zip", "d:\temp");
                rec.Return(outputPath);
                FileRoutines.Delete("file1");
                FileRoutines.Delete("file2");

            }
            fileManager.Register("file1");
            fileManager.Register("file2");

            fileManager.ZipFilesTo(outputPath);
        }

Mocking External zip library.

The second:
        [Test]
        public void GetObjectId_EntityAndDbNameAreInDb_ReturnsPropId()
        {
            ObjectsBc manager = new ObjectsBc();
            InvBaseEntity entity = InvBaseEntity.Create("Жилое здание", 100);
            SqlParameter param = new SqlParameter("param1", "value");
            using (RecordExpectations rec = RecorderManager.StartRecording())
            {
                SqlExecuter executer = SqlExecuter.CreateSPCommand("GetObjectId");
                rec.CheckArguments();

                executer.AddParameter(param); 
//same error
// code is command.Parameters.Add(param); 
                executer.AddParameter(param);

                executer.ExecuteScalar();
                rec.Return(50);
                executer.Dispose();
            }


            Assert.AreEqual(50, manager.GetObjectId(entity, "Балтийск"));
        }

here I just made my void AddParam(SqlParameter func); to return 0; and added rec.Return(0) to fix this problem.
answered by Gmoorick (2.3k points)
0 votes
You should not need to return a fake value.
Try rec.ReturnDefaultImplementation()

You have found a bug :twisted:
thanks for the examples.
We will fix it and send you a patch
answered by scott (32k points)
0 votes
Hi,

This issue has been resolved.
The fix was included in our latest release (4.0.2),
which can be loaded here: https://www.typemock.com/Downloads.php


Mocking Methods returning void should work now without any errors.
answered by lior (13.2k points)
...