Skip to content

Instantly share code, notes, and snippets.

@themoxman
Last active January 4, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themoxman/8673084 to your computer and use it in GitHub Desktop.
Save themoxman/8673084 to your computer and use it in GitHub Desktop.

Availability of set_user

Think I'm missing something obvious here, but why is set_user available to be called wherever I want in CollectionsController?

2.0.0p353 :009 > class Test
2.0.0p353 :010?>   def self.method1
2.0.0p353 :011?>     method2
2.0.0p353 :012?>     end
2.0.0p353 :013?>   def method2
2.0.0p353 :014?>     puts "method2 called"
2.0.0p353 :015?>     end
2.0.0p353 :016?>   end
 => nil
2.0.0p353 :017 > Test.method1
NameError: undefined local variable or method `method2' for Test:Class
	from (irb):11:in `method1'
	from (irb):17
	from /Users/davemox/.rvm/gems/ruby-2.0.0-p353@md_notes/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
	from /Users/davemox/.rvm/gems/ruby-2.0.0-p353@md_notes/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
	from /Users/davemox/.rvm/gems/ruby-2.0.0-p353@md_notes/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'
2.0.0p353 :018 >

Testing set_user

Any thoughts on how to test the set_user method?

Here's what I did, it's pretty messy though:

  describe "set_user" do
    it "sets @user to the currently signed in user" do
      valid_params = { collection: { name: "Derp", description: "Derpity derp derp"} }
      post :create, valid_params
      expect(assigns(:user)).to eq(@user)
    end

Controller Helper methods

Should set_user in my CollectionsController be in a different file? ApplicationController for example? Why should it be a private method (reference Rails 4 in Action).

####Heroku deployment -- making config vars available at compile time

There a different way to solve the below? I used a Heroku Labs option to make the env vars available during the build.

Kept getting this error when I tried to push my Devise updated app to Heroku:

Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       Devise.secret_key was not set. Please add the following to your Devise initializer:
       config.secret_key = '369f1a50f24bb3684c9ccffc6e83cbe6673bd617c80883bae0c3734f50d21be4ee4f96ab1c54596f910d5f67a59b23de56c8022c8b18b9fda5ca2018a78b1f96'
       Please ensure you restarted your application after installing Devise or setting the key.
       /tmp/build_6fc8f03f-29d7-460a-91ce-cb7dcedea18b/vendor/bundle/ruby/2.0.0/gems/devise-3.2.2/lib/devise/rails/routes.rb:469:in `raise_no_secret_key'

Resolving Error:

####User Sign-in for feature specs (Devise)

So I just created this helper method and put it in a support file. I can call sign_in_user at any point in my feature specs. Would you just directly access the session to avoid all this click around balogne?

module Features
  module SessionHelpers
    def sign_in_user
      User.create( email: "dave@example.com", password: "randomnums" )
      visit new_user_session_path
      fill_in 'user[email]', with: 'dave@example.com'
      fill_in 'user[password]', with: 'randomnums'
      click_button 'Sign in'
    end
  end
end

####ActiveRecord query magic? How in the world does this work?

tag_list = ["1,", "4,", "8,"]
Tag.find(tag_list)

Does ActiveRecord remove the commas in each array object and then make the query??

####Best way to test Sessions?

I have unit tests for the SearchController#update method that handles the session, but am not sure about acceptance or integration testing at a higher level. And, apparently you're not supposed to use Capybara for testing HTTP verbs, see this blog post.

feature "Find notes" do
  scenario "view previous search results utilizing sessions", js: true, feature: true do
    # make a POST search and populate session[:tag_list]
    visit new_search_path
    click_button 'Rails'
    click_button 'Postgres'
    click_button 'Ruby'
    click_button 'Postgres'
    click_button 'Find Notes'
    post_page = page
    # make a GET search and test for results
    visit results_search_path
    puts "a get request? #{request.get?}"
    get_page = page
    expect(post_page).to eq(get_page)
  end
end

####Testing Search Results

I have a number of tests in my search_controller_spec.rb file that test my apps query functions. I'm not sure the best way to do this. For some reason today, rails started assigning @notes to [@postgres, @rails'] instead of [@rails, @postgres] which it had been doing previously. Not sure what changed in my code that caused this, but it got me thinking about what the best way to test equality of expected and returned search results.

context "POST #results" do
  it "assigns notes, associated with params[:tag_list], to @notes", unit: true do
    post :results, { tag_list: @tag_ids }
    expect(assigns(:notes)).to eq([@postgres, @rails])
  end
end  

This happened two other places too. I don't understand why AR is now returning collections in reverse order (by id). Happened in notes_controller_spec.rb line 45 and in search_controller_spec.rb line 72.

Module or Class

Should my TagHandler and SearchHandler classes be modules that are included into the NoteController and SearchController? Or maybe it should be extracted out into a controller helper module. I mean I have that entire directory app/helpers full of modules. Should I be using those?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment