Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Drop Down List

0 votes

I've created the unit test below and I want to set the value of the drop down control to "R" but using SelectedValue doesn't seem to work.  How do I set the selected value of a drop down list in a test?

        [Test]
        public void Button1_Click_DropDown()
        {
            // arrange
            var webForm1 = new WebForm1();
            var dropDownList = new DropDownList();
            var sender = new object();
            var eventArgs = new EventArgs();
 
            dropDownList.SelectedValue = "R";
            Isolate.NonPublic.InstanceField(webForm1, "ddlRating").Value = dropDownList;
 
            // act
            Isolate.Invoke.Method(webForm1, "Button1_Click", sender, eventArgs);
 
            // assert
            Assert.AreEqual("Ticket Price: $18", (string)Isolate.Invoke.Method(webForm1, "GetPrice", "rating"));
        }
closed with the note: Exactly the answer I was looking for.
asked Oct 21, 2016 by chking999 (170 points)
closed Oct 25, 2016 by chking999

1 Answer

+1 vote

Hello chinking,

Its hard to know what is the issue of the test without seeing the code,

but perhaps this will solve your problem:

insted of  "dropDownList.SelectedValue = "R";" you can use :

Isolate.WhenCalled(() => dropDownList.SelectedValue).WillReturn("R");  "

 

Cheers,

Daniel

Typemock supprot,

answered Oct 25, 2016 by Daniel (1,340 points)
...