كيفية التحقق مما إذا كانت طريقة المساعد/المتغير موجود في RSPEC؟
سؤال
ما زلت أحاول اكتشاف RSPEC والآن أنا أستخدم AuthLogic للتعامل مع المستخدمين والجلسات. لقد فعلت الأشياء المعتادة AuthLogic مثل إضافة طريقة DEF Current_user إلى وحدة تحكم Applciation ، وكطريقة مساعد ، ولكن كيف يمكنني الوصول إلى ذلك في ملف وحدة تحكم RSPEC الخاص بي؟
إليكم user_sessions_controller_spec.rb:
require 'spec_helper'
require 'authlogic'
describe UserSessionsController do
context "user is already logged in" do
before(:each) do
include Authlogic::TestCase
activate_authlogic
UserSession.create Factory.build(:user)
end
it "should redirect the user to the home page" do
get 'new'
response.should redirect_to(home_path)
end
end
describe "#create" do
context "when the user is not logged in" do
before(:each) do
current_user = nil
end
it "correct authorization should create a new session" do
post 'create', {:login => "afactoryuser", :password => "apass", :password_confirmation => "apass"}
current_user.should_not be_nil
end
end
end
end
عندما أقوم بتشغيل RSPEC ، يخبرني فقط:
correct authorization should create a new session
undefined local variable or method `current_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x000001017d3410>
لذا ، أعتقد أنه في سياق RSPEC ... لكن كيف أعرف أنه يجب أن يكون في Sessions_Controller؟ وهل أنا حتى أختبرها بشكل صحيح؟
المحلول
insted of current_user في RSPEC ، حاول controller.current_user
إذا كنت ترغب في استخلاص طريقة Current_user ، فاستخدم: controller.stub!(:current_user).and_return(whatever)
في before :each
حظر ، ولكن بعد ذلك سوف تحصل دائمًا whatever
إذا قمت بتعطيلها.
لا تنتمي إلى StackOverflow