简体   繁体   中英

Assert.AreEqual on a property that only has a Setter, unit testing in MVP

I have an issue when trying to assert whether a property is equal to a number when the property only has a setter.

Here is my view interface:

public interface ICalculatorView
    {
        int Number1 { get; }
        int Number2 { get; }
        string MathmaticalCalculation { get; set; }
        int Answer { set; }
    }

I only display the Answer on screen and don't use the get accessor, that's why it only has the set accessor on the property.

Here is my view that implements the interface:

CustomerDetailsPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new CustomerDetailsPresenter(this, this, 
                new GetCustomerDetailsModel());
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                customerIdDDL.DataSource = ListOfCustomerIds;
                customerIdDDL.DataBind();
            }
        }
        protected void performCalcButton_Click(object sender, EventArgs e)
        {
            MathmaticalCalculation = ((Button)sender).Text;
            _presenter.PerformCalculation();
        }

        public int Number1
        {
            get { return int.Parse(number1TextBox.Text); }
        }

        public int Number2
        {
            get {  return int.Parse(number2TextBox.Text);}
        }

        public string MathmaticalCalculation { get; set; }  

        public int Answer
        {
            set { answerLabel.Text = value.ToString(); }
        }
}

I want to be able to unit test the perform calculation method in a unit test. Here is the test method:

[TestMethod]
        public void TestCalcWorks_AddingTwoNumbers()
        {
            // Arrange
            var mockIDetailsView = new Mock<ICustomerDetailsView>();
            var mockICalculatorView = new Mock<ICalculatorView>();
            var mockIGetCustomerDetailsModel = new Mock<IGetCustomerDetailsModel>();
            var mockTestPresenter = new Mock<CustomerDetailsPresenter>();

            var testPresenter = new CustomerDetailsPresenter(mockIDetailsView.Object,
                mockICalculatorView.Object, mockIGetCustomerDetailsModel.Object);

            mockICalculatorView.Setup(x => x.Number1).Returns(2);
            mockICalculatorView.Setup(x => x.Number2).Returns(2);
            mockICalculatorView.Setup(x => x.MathmaticalCalculation).Returns("+");

            // Act
            testPresenter.PerformCalculation();
            // Assert

            Assert.AreEqual(mockICalculatorView.Object.Answer, 4);
        }

This will not compile as The mockICalculatorView.Object.Answer does not have a get accessor. I am aware I can add this to the Answer property and everything would work fine. But surely, I don't need to modify any code I have written just so that I can run the test method, or do I?

It looks like you're using Moq. You could do this instead (for the assertion):

mockICalculatorView.VerifySet(m => m.Answer = 4, Times.Once());

If you want to test if Answer is 4, of course you have to have some way of getting the result. If the value is never saved anywhere, it can't be retrieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM