There is a class whose constructor initializes the local object:

RSZKmodelManager_201638.cs

public class RSZKmodelManager_201638 { private RSZKmodel model; public RSZKmodelManager_201638(Excel.Range rows) { model = new RSZKmodel(); int number = 0; foreach (Excel.Range row in rows) { model.Exercises.Add(new Exercise { Number = number++, Parts = row.Cells[1, 1].Value2.ToString(), ... }); } } } 

Now in the unit test I would like to test this constructor and check whether initialization is correct:

TestRSZKmodelManager_201638.cs

 [TestClass] public class TestRSZKmodelManager_201638 { private IEnumerator RowCollection() { var row1 = new Mock<Excel.Range>(); var row2 = new Mock<Excel.Range>(); row1.Setup(x => x.Cells[1, 1].Value2).Returns("something"); //здесь VS показывает ошибку yield return row1.Object; yield return row2.Object; } [TestMethod] public void TestPopulateModel() { // Arrange var moqRange = new Mock<Excel.Range>(); moqRange.Setup(x => x.GetEnumerator()).Returns(RowCollection); //Act RSZKmodelManager_201638 manager = new RSZKmodelManager_201638(moqRange.Object); // Assert //Assert.IsNotNull(manager); } } 

Wednesday on the row row1.Setup(x => x.Cells[1, 1].Value2).Returns("something"); shows:

enter image description here

The problem is that the Range.Value2 property Range.Value2 dynamic. Without this, I would somehow get out.) Perhaps Moq does not support such properties? I read something about the Rhino.Mocks framework, but I never used it.

How do I properly lock an Excel.Range object in general?

  • Write a specific error text, just "fail" is not enough. - Monk

1 answer 1

You have a confusion in the installation. In the code you use Excel.Range as follows:

  1. Rows to the Rows property.
  2. Each row refers to the Cells property at index 1,1 .

And in the test you wash the call to Rows at index 1,1 .

You need:

  1. Hide the return value of the Rows property.
  2. Each returned row has a Cells property.
  3. For each returned cell, block the reference to the indexer at index 1,1 .
  • It turned out that it was necessary to first implement an IEnumerator . Now the problem is different. Question updated. - Adam