Skip to content

Instantly share code, notes, and snippets.

@averygoodplanet
Last active May 11, 2020 05:29
Show Gist options
  • Save averygoodplanet/9237921 to your computer and use it in GitHub Desktop.
Save averygoodplanet/9237921 to your computer and use it in GitHub Desktop.
How to simulate devise's current_user variable in rspec unit tests?
class Budget < ActiveRecord::Base
belongs_to :user
has_many :categories, dependent: :destroy
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :budgets, dependent: :destroy
def create_new_budget(name)
budget = Budget.create(name: name, user: current_user)
end
end
require 'spec_helper'
describe User do
describe "#create_new_budget" do
before(:each) do
@current_user = Fabricate(:user)
@current_user.create_new_budget(name: "new budget")
end
it "should create a new budget under current_user with a budget name" do
@current_user.budgets.where(name: "new budget").count.should equal(1)
end
it "should create a budget with income of zero" do
@current_user.budgets.where(name: "new budget")[0].income should equal(0)
end
it "should create the ten (10) categories under the new budget" do
@current_user.budgets.where(name: "new budget")[0].categories.count should equal(10)
end
it "the new budget's categories should have the default 10 category names" do
categories = Budget.first.categories
category_names = []
for i in categories
category_names.push(i.name)
end
category_names.should =~ ["Saving", "Housing", "Utiilities", "Food",
"Transportation", "Clothing", "Medical/Health", "Personal", "Recreation", "Debts"]
end
end
end
@drewblumberg
Copy link

Now all you need to do is post the right params to the create route for budget to create a budget tied to that user.

@seanski
Copy link

seanski commented Feb 27, 2014

It looks like you're just missing sign_in @current_user. It should look like this

before(:each) do
  @current_user = Fabricate(:user)
  @current_user.create_new_budget(name: "new budget")
  sign_in @current_user
end

@seanski
Copy link

seanski commented Feb 27, 2014

Oh, also, you can't use current_user in your model. current_user is a controller method.

Instead, you could do:

def create_new_budget(name)
    self.budgets.create(name: name)
 end

Which will create a new budget within the scope of the User instance and return it.

@averygoodplanet
Copy link
Author

The devise test helpers are for Controller tests that need a signed in / authenticated user.

What you have in your gist is simply a User model, and a spec for that. It is outside of devise’s context (the controller). I would first, in irb, make sure you understand how to create a user in the db, with a linked budget. Treat it like you are testing any other model, I don’t think devise is related here.

(Raw Syntax)

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