Skip to content

Instantly share code, notes, and snippets.

@themoxman
Last active January 2, 2016 03:49
Show Gist options
  • Save themoxman/8246427 to your computer and use it in GitHub Desktop.
Save themoxman/8246427 to your computer and use it in GitHub Desktop.
  • I'm looking at rake tasks such as rake db:create:all. I have no idea what that does, so I look at the databases.rake active_record file. Should I understand what's going on here? How would you approach trying to understand what this code does. Not only do you need to understand this code, but even more so, you have to dig around into other source code. Not sure where to start.

    • use -w
  • Rspec output

11:42:14 davemox@Daves-MacBook-Pro $ bundle exec rspec
F

Failures:

  1) Add a new note without tags
     Failure/Error: }.to change(Note, :count).by(1)
     NameError:
       uninitialized constant Note
     # ./spec/features/new_note_spec.rb:10:in `block (2 levels) in <top (required)>'

Finished in 0.64077 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/new_note_spec.rb:4 # Add a new note without tags

Randomized with seed 7325

That's not a very helpful error message. Is there some setup I'm missing?

  • Do you use the haml-rails gem?

  • So you mentioned not using bundler yesterday. I forget how you handle gem dependencies. Also, you do or don't use RVM?

  • What's a good commit size? RAMBLINGS ALERT. Should I be committing groups of tests I do separately? For instance commit all the 'with valid params' NotesController#create test separately from the 'with invalid params' tests? Is everything that causes a feature spec to pass considered a single commit or are bits and pieces of the code that causes the feature spec to pass committed separately?

  • What in the world is going on here? Validations work even without checking if @note.save returns true. And what's even more confusing is when I click 'save', no request is made, a chrome validation div pops up and says, "hey, make sure this is filled in".

  def create
    @note = Note.new(note_params)
    # good discussion of rencer vs redirect_to http://blog.markusproject.org/?p=3313
    # also, see pg18 of 'Rails 4 in Action'
    # if @note.save
    #   redirect_to new_note_path, notice: "Note created"
    # else
    #   # note
    #   # render action: 'new'
    #   redirect_to notes_path
    # end
    @note.save
    redirect_to new_note_path
  end
  • Obviously I could use something like Factory girl, but how do you handle test objects such as 'tags' below?
feature "Tags" do
  scenario "show all tags" do
    tags = %w(Rails Ruby ActiveRecord Haml)
    tags.map! { |tag| Tag.create(name: tag) }
    visit root_path
    click_link "Tags"
    expect(page).to have_content("Haml")
  end
end
  • Test DB object ID's continue to rise. If the DB is cleaned out prior to each test running, why does my test Tag have an id of 38?
Failures:

  1) TagsController GET #edit assigns the requested tag to @tag
     Failure/Error: expect(assigns(:tag)).to eq tag

       expected: #<Tag id: 38, name: "RSpec", created_at: "2014-01-09 01:00:28", updated_at: "2014-01-09 01:00:28">
            got: nil

       (compared using ==)
     # ./spec/controllers/tags_controller_spec.rb:24:in `block (3 levels) in <top (required)>'

  Finished in 0.04751 seconds
  4 examples, 1 failure
  • save_and_open_page behavior
scenario "edit a tag" do
  visit tags_path
  click_link "SimpleForm"
  # add space between 'Simple' and 'Form'
  fill_in 'tag[name]', with: 'Simple Form'
  click_button 'Save Changes'
  save_and_open_page # this works
  expect(current_path).to eq tags_path
  expect(page).to have_content("Simple Form")
  save_and_open_page # this does not work
  expect(page).to have_content("Tag updated")
end
  • I don't understand the patch :update ... request. what is id: @tag and name: @tag. This SO post is helpful, looks we're just trying to pass #update a params hash like normal. I just don't understand the syntax here.
      it "finds the correct Tag from the DB" do
        # do i need `name: @tag` here? since all i'm really looking for is that #update returns the correct object
        # how is this first step different from #edit above?
        patch :update, id: @tag, name: @tag
        expect(assigns(:tag)).to eq(@tag) 
      end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment