I've checked all the SO questions I can find related to this issue.
The rest of the form works and the test passes if I remove the validation that a PlanTemplate must have a Category.
I have created a Category with a name of 'Planets' in the Test script elsewhere.
I've just got Capybara screenshot working and have now updated it to include the text - it doesn't seem to be appearing in Capybara, but is appearing in local - I can't tell from my code why it would be hidden?
Full error:
1) PlanTemplate can be created by admin admin can create new plan
Failure/Error: expect(page).to have_content('Planets')
expected to find text "Planets" in "window.addEventListener(\"load\", function(){ window.cookieconsent.initialise({ \"palette\": { \"popup\": { \"background\": \"#252e39\" }, \"button\": { \"background\": \"#428bca\" } }, \"theme\": \"classic\", \"content\": { \"href\": \"Toggle navigation Communities Communities People Mates Weekly Activity view Recent Activity list view All Mate Goals Manage Mates Plans Goals All Goals Manage My Goals New Goal Admin Admin Account Profile Email Settings Manage Mates My Interests Log out New Plan Template Plan name: Plan description: What additional information is required? (optional) What is the delivery medium & requirements of plan? (optional) What should you do once the plan is complete? (optional) How many hours per week does this goal take to complete? How many days in total does this take to complete? /7 for weeks. This will be used to calculate start times What level of user is this plan for? beginner novice intermediate experienced expert What category is this goal in? Feed My Goals My Mates All goals Home Sign Up Log in Browse Users Feedback © Browse Goals Privacy TOS Cookies window.setTimeout(function() { $(\".alert\").fadeTo(500, 0).slideUp(500, function() { $(this).remove(); }); }, 6000);"
Things I have tried to date are commented out below (have also tried others and removed them):
let(:category) { create :category, name: 'Planets', id: 99 }
scenario 'admin can create new plan' do
login_as(create(:admin))
visit("/plans/new")
find('#plan-name').set('A test plan name')
find('#plan-desc').set('A test plan description, I think these need to be longish')
find('#plan-weekly-hours').set(1)
# page.check('Planets')
# find("label[for='Planets']").click
# find_field(['plan_template_category_ids_1']).check
# find('.checkbox').check
# check('Planets')
# find(:label, 'Planets').click
# check('.checkbox', allow_label_click: true)
# find(:label, 'plan_template_category_ids_1').click
# find('#plan_template_category_ids_1', visible: false).trigger('click')
find('#plan-days-to-complete').set(35)
find('#submit-new-plan').click
expect(page).to have_content('Plan template was successfully created.')
expect(page).to have_content('A test plan description, I think these need to be longish')
end
I have also added to rails_helper:
Capybara.automatic_label_click = true
Capybara.ignore_hidden_elements = false
The form partial code in question:
<%= form.label "What category is this goal in?" %><br>
<%= form.collection_check_boxes :category_ids, Category.all, :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
<% end %>
The html snapshot from Capybara screenshot - have to paste it as an image as SO doesn't like it in code.
How it displays in the DEV browser *not test:
<div class="col-md-12 goalform">
<label>What category is this goal in?</label><br>
<input type="hidden" name="plan_template[category_ids][]" value=""><label class="checkbox-inline input_checkbox" for="plan_template_category_ids_2">
<input class="checkbox" type="checkbox" value="2" name="plan_template[category_ids][]" id="plan_template_category_ids_2">Dream Chasing</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_1"><input class="checkbox" type="checkbox" value="1" name="plan_template[category_ids][]" id="plan_template_category_ids_1">Weightloss</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_4"><input class="checkbox" type="checkbox" value="4" name="plan_template[category_ids][]" id="plan_template_category_ids_4">Productivity</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_11"><input class="checkbox" type="checkbox" value="11" name="plan_template[category_ids][]" id="plan_template_category_ids_11">Popular</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_3"><input class="checkbox" type="checkbox" value="3" name="plan_template[category_ids][]" id="plan_template_category_ids_3">Fitness</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_12"><input class="checkbox" type="checkbox" value="12" name="plan_template[category_ids][]" id="plan_template_category_ids_12">Health</label>
</div>
This could've been diagnosed by using a debugging tool (e.g. using the pry gem and adding 'binding.pry' inside of the test (and before the failing expect line) which would have shown me that the Categories were not being generated correctly due to the lazy loading of the 'let'