Learner
Learner

Reputation: 990

How to pass mock object to another mock object constructor

    /// <summary>
    ///A test for ReverseName
    ///</summary>
    [TestMethod()]
    public void ReverseNameTest()
    {
        Mock<IEntityName> entityName = new Mock<IEntityName>();
        entityName.SetupProperty(x => x.FirstName, "John");
        entityName.SetupProperty(x => x.LastName, "T.");

        var p = new Person(entityName.Object);

        string expected = "Your reverse name is T. John"; 
        string actual;
        actual = p.ReverseName();
        Assert.AreEqual(expected, actual);
    }
}

//Person Class

  public Person(IEntityName EntityName)
    {
        this.EntityName = EntityName;
    }

Is it possible to mock Person class as well in the TestMehod or do I have to create an instance of Person as above?

Upvotes: 0

Views: 3027

Answers (1)

Stay Foolish
Stay Foolish

Reputation: 3736

yes, you can. Rhino mock supports that, not sure about Moq, but i think you can do that as well

Take a look at this thread. Passing Moq mock-objects to constructor

Mocking objects with Moq when constructor has parameters

Upvotes: 1

Related Questions