I am trying to write a SimpleTest for a form that already exists. The form passes tests for GET, valid HTML and having the select element, which looks like this:

<select id="edit-buid" name="buid" class="form-select">
  <option value="1">New Business Unit</option>

The relevant code from my test case is:

$this->assertFieldByName('buid', NULL, 'form has buid field');

$edit = array(
  'buid' => 1,

$this->drupalPost('path/to/form', $edit, t('Save'));

I have tried passing the string "1" and array(1) and array("1") as well, but the test always fails with:

"Failed to set field buid to 1"

How can I make the test pass with the correct option selected?

  • Hi Joe, have you previously fetched the form that you are trying to submit to using get? Basically, is the previous drupalGet or drupalPost to the same url as the form you are trying to post to? – Daniel Yule Apr 3 '12 at 15:36
  • Thanks, Daniel. I GET the form right before this and verify that the element exists. I added those lines to my code above. – Joe Beuckman Apr 3 '12 at 15:40
  • 1
    I've re-created the situation you've posted above, and it is working fine for me. I am able to set the select element and pass all the tests. I am wondering if you have some kind of typo somewhere or alternately if your form has a different field also called 'buid', or something. I personally made a typo when I was re-creating it, and called the select element 'build' which did not make the test happy. – Daniel Yule Apr 3 '12 at 15:47
  • Daniel, thanks for your responses and verification. I figured out that my select did not have the option that I thought it did. I had to insert a couple of db records in the test case so that my form code would fill the select with an appropriate option to be selected. – Joe Beuckman Apr 3 '12 at 16:06
  • Remember to add that as an answer or close the question. – Berdir Apr 4 '12 at 17:53

After talking this through, I realized that I had not created the database records necessary to fill the select element in the test case. So, during the test, there actually was no <option value="1">. The lesson is to think carefully about the entire context when writing test cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.