1
votes

There is the following RSpec code:

describe 'with valid information' do
    it 'should create a new restaurant' do
      visit new_restaurant_path

    fill_in I18n.translate(:name),          with: "Some restaurant"
      fill_in I18n.translate(:address),       with: "Some address of the restaurant"
      fill_in I18n.translate(:average_check), with: 100

    check('place_restaurant_food_types_1')

    expect { click_button :submit }.to change(Restaurant, :count).by(1)
    end
end

But I always get error "cannot check field, no checkbox with id, name, or label 'place_restaurant_food_types_1' found". I tried to replace id for name, but it was still the same. I'm absolutely sure that there is the item with needed id! (I copied it from the page source). How can I fix it?

HTML:

<form accept-charset="UTF-8" action="/restaurants" class="new_place_restaurant" id="new_place_restaurant" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="7yQCirO7MLO6e+Nj46eCSNUjQWd67MJVDIHmUV6/5Y0=" /></div>
        <div class="row">
  <label for="place_restaurant_name">Название</label> 
  <input id="place_restaurant_name" name="place_restaurant[name]" size="30" type="text" />
</div>
<div class="row">
  <label for="place_restaurant_address">Адрес</label> 
  <input id="place_restaurant_address" name="place_restaurant[address]" size="30" type="text" />
</div>

        <div class="row">
            <label for="place_restaurant_average_check">Средний чек</label>
            <input id="place_restaurant_average_check" name="place_restaurant[average_check]" size="30" type="text" />
        </div>
            <div class="row">
                <input id="place_restaurant_food_types_1" name="place_restaurant[food_types][1]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_1">Восточная кухня</label>      
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_2" name="place_restaurant[food_types][2]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_2">Национальная кухня</label>       
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_3" name="place_restaurant[food_types][3]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_3">Японская кухня</label>       
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_4" name="place_restaurant[food_types][4]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_4">Китайская кухня</label>      
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_5" name="place_restaurant[food_types][5]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_5">Европейская кухня</label>        
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_6" name="place_restaurant[food_types][6]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_6">Кавказская кухня</label>     
            </div>
        <div id="map_canvas"></div>
<input id="latitude" name="place_restaurant[latitude]" type="hidden" value="54.352271" />
<input id="longitude" name="place_restaurant[longitude]" type="hidden" value="48.52652" />


        <div class="row">
            <input id="submit" name="commit" type="submit" value="Сохранить" />
        </div>
</form>
3
Can you add the snippet of the Source code (html) here? That would helpSunita Venkatachalam
How did you get that source? I'd suggest looking at what Capybara sees - immediately before your test assertion, add puts page.html and amend your question with that.Shepmaster

3 Answers

1
votes

You could just use

check("place_restaurant_food_types_1")

as shown in the docs:

The check box can be found via name, id or label text.

0
votes

Try By Xpath like this

find(:xpath, "//*[@id='place_restaurant_food_types_1']").click
0
votes

You can use like this

find(:css,"input[id='place_restaurant_food_types_1']").set true