Last active
May 11, 2020 05:29
-
-
Save averygoodplanet/9237921 to your computer and use it in GitHub Desktop.
How to simulate devise's current_user variable in rspec unit tests?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Budget < ActiveRecord::Base | |
belongs_to :user | |
has_many :categories, dependent: :destroy | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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.
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
It looks like you're just missing
sign_in @current_user
. It should look like this