This is a migrated thread and some comments may be shown as answers.

passing the mocked arrangement in as an Expression<Action>...

2 Answers 233 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Arthur
Top achievements
Rank 1
Arthur asked on 18 Sep 2020, 11:09 PM
Was looking for ways to pull the lambda expressions I mock up....this way I could reuse them during the assertion portion of my test.

However when I pass the value in via a variable I get a test exception:
Telerik.JustMock.Core.MockException: The arranged function is not set up to return a value of type Account


Here is a sanitized version of what I'm trying to do?


public Interface IAccountManagement{
   public Account GetAccount(string acctId);
}
........

public void MyTest()
{
   IAccountManagement actMgrMock = Mock.Create<IAccountManagement>();
   Account acct = TestHelpers.AutoGenerateAccount;  //generate an acct object with Ploeh.AutoFixture

   Expression<Action> GetAccount = () => actMgrMock.GetAccount(Arg.IsAny<string>());

  //Mock.Arrange(GetAccount).Returns(e);   //<------this call will not work when I invoke the test?

  Mock.Arrange(() => actMgrMock.GetAccount(Arg.IsAny<string>())).Returns(e); //  This one works?

  // invoke
  
  //Assert
  //Mock.Assert(GetAccount,Occurs.Once());

}
  


2 Answers, 1 is accepted

Sort by
0
GiZ
Top achievements
Rank 1
answered on 21 Sep 2020, 08:12 AM
I think the expression has to be of type Expression<Func<Account>> instead of Expression<Action>.
0
Ivo
Telerik team
answered on 21 Sep 2020, 08:31 AM

Hello Arthur,

The issue is caused by the generic argument used in Expression. The arranged method returns a value, so replacing Action with Func<Account> solves the problem. The test code could be changed as following:

[TestMethod]
public void MyTest()
{
    IAccountManagement actMgrMock = Mock.Create<IAccountManagement>();
    Account acct = TestHelpers.AutoGenerateAccount;  //generate an acct object with Ploeh.AutoFixture

    // Arrange
    Expression<Func<Account>> GetAccount = () => actMgrMock.GetAccount(Arg.IsAny<string>());
    Mock.Arrange(GetAccount).Returns(acct);

    // Act
    actMgrMock.GetAccount("fakeId");

    //Assert
    Mock.Assert(GetAccount, Occurs.Once());
}

I hope the provided information helps. If you have further questions do not hesitate to write us back.

Regards,
Ivo
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Arthur
Top achievements
Rank 1
Answers by
GiZ
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or