Skip to content

Instantly share code, notes, and snippets.

@lingtran
Forked from rrgayhart/1602-testing-homework.markdown
Last active July 1, 2016 20:43
Show Gist options
  • Save lingtran/c39a0d7a010fbbee7a6757f27cfc4084 to your computer and use it in GitHub Desktop.
Save lingtran/c39a0d7a010fbbee7a6757f27cfc4084 to your computer and use it in GitHub Desktop.
Testing Homework

Testing Homework - Rails/JS

  • Choose one of the following tracks:
  • Skim/Read through the associated links
  • Attempt to hook up and implement unit or feature js tests in your IdeaBox or this sample idea-bin project
  • Fork this gist

Responses for feature testing

  • Experience implementing:

    • The setup for testing has been the trickiest I've experienced to date. The process of figuring it out was frustrating and I suspect largely because I am not entirely clear how a Rails app with JavaScript interacts with the browser under testing. I got data to load via wait_for_ajax and setting up the driver. So my feature tests for viewing all ideas and adding a new idea passed.
    • However, in terms of my feature test to delete an idea, despite witnessing the successful behavior of deletion, my assertion for expecting a different in count in the database for the Ideas table kept failing. It seems the test database stayed at zero. The test was running off of development data. The current way I'm testing deletion is flawed -- I am manually creating new data in the form and then deleting the first one -- but this was my workaround because even when I utilized factories for test data, the Idea.count in rails c test was still 0. I tried adding configurations and appropriate strategies in my database_cleaner configurations. This did not resolve the issue. Thoughts/recommendations?
  • Were you successful?

    • yes and no -- see response for experiencing implementing the feature tests
  • Links to commits on Github or copy and pasted code snippits of a test:

    • https://github.com/lingtran/idea_box/commit/697692768410106073acf38a7699cdb576301e04

    • code snippet for a success feature test:

        RSpec.describe "User can view ideas", type: :feature do
          scenario "user can view all existing ideas", :js => true do
            swill_idea = create(:idea)
            genius_idea = create(:idea, :genius_idea)
        
            visit root_path
            wait_for_ajax
        
            within(".ideas-index-headers") do
              expect(page).to have_content("Title")
              expect(page).to have_content("Body")
              expect(page).to have_content("Quality")
              expect(page).to have_content("Delete")
            end
        
            within(".ideas-table") do
              expect(page).to have_css('.ideas-list')
              expect(page).to have_css('.idea-title')
              expect(page).to have_css('.idea-body')
              expect(page).to have_css('.idea-quality')
              expect(page).to have_css('#thumbs-up')
              expect(page).to have_css('#thumbs-down')
              expect(page).to have_content(swill_idea.quality)
              expect(page).to have_content(genius_idea.quality)
            end
          end
        end
      
    • code snippet for failing/skipped test:

        RSpec.describe "User can delete idea", type: :feature do
          xscenario "they can delete an idea", :js => true do
            new_idea = { title: "New idea title", body: "New idea body" }
        
            visit root_path
        
            3.times do
              within('.new-idea') do
                fill_in "Title", with: new_idea[:title]
                fill_in "Body", with: new_idea[:body]
                click_button("Save")
              end
            end
        
            original_count = page.all('.ideas-list').length
        
            wait_for_ajax
        
            expect{within('.ideas-index') do
              page.all('.delete-idea')[0].click
            end}.to change{Idea.count}.by(-1)
        
            visit current_path
        
            wait_for_ajax
        
            new_count = page.all('.ideas-list').length
        
            expect(new_count).not_to eq(original_count)
          end
        end
      
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment