chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
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!
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 by (9.1k points)
closed by

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 by (1.3k points)
...