####Strange Test Behavior
With no apparent order, the test will sometimes pass and sometimes not pass.
~/Desktop/coding_resources/programs/md_notes
23:36:02 davemox@Daves-MacBook-Pro $ bundle exec rspec
.........................................
Finished in 6.33 seconds
41 examples, 0 failures
Randomized with seed 44807
-------------------------
~/Desktop/coding_resources/programs/md_notes
23:36:58 davemox@Daves-MacBook-Pro $ bundle exec rspec
.....................F...................
Failures:
1) SearchController POST #results assigns all notes, associated with params[:tags_list], to @notes
Failure/Error: post :results, { tag_list: tag_ids }
ActiveRecord::RecordNotFound:
Couldn't find all Tags with IDs (3, 4, 16) (found 0 results, but was looking for 3)
# ./app/controllers/search_controller.rb:16:in `results'
# ./spec/controllers/search_controller_spec.rb:51:in `block (3 levels) in <top (required)>'
Finished in 7.06 seconds
41 examples, 1 failure
Failed examples:
rspec ./spec/controllers/search_controller_spec.rb:49 # SearchController POST #results assigns all notes, associated with params[:tags_list], to @notes
Randomized with seed 8682
####Search querying method placement Would you place the SearchController#results query stuff in a different method? And where would you put it? One of the models? This is the code I'm talking about:
def results
tag_list = params[:tag_list].strip.split
@notes = Tag.find(tag_list).map { |x| x.notes.to_a }.flatten.uniq
end
####Feature spec for adding note with tags
So my feature spec scenario "create a note and tag it"
basically does the exact same thing as the spec above it, scenario "create a note"
except I'm now selecting some tags to add to the Note. The unit tests in the controller/model will cover the real guts of making sure the tag to note association is made. Should this feature be squashed into the first one, the 'create a note' one? Or should they be separate. Here are the two tests:
feature 'Add a new note' do
# don't need to say "without tags" because tags aren't a feature
scenario "create a note" do
visit root_path
# don't test that Note count +1, that's internals
fill_in 'note[title]', with: 'This is a sample note'
fill_in 'note[content]', with: 'Content for our sample note'
click_button 'Add Note'
# don't need this since if i redirect to the correct page, I'll see the notice
expect(current_path).to eq new_note_path
expect(page).to have_content "Note created"
end
scenario "create a note and tag it" do
visit root_path
fill_in 'note[title]', with: 'ActiveRecord and Postgres'
fill_in 'note[content]', with: 'A note about using Postgresql with ActiveRecord'
click_button 'Rails'
click_button 'Ruby'
click_button 'Postgres'
click_button 'Ruby'
click_button 'Add Note'
expect(current_path).to eq new_note_path
expect(page).to have_content "Note created"
end
end