rspec 测试期间如何在回调中传递变量?
How to pass a variable in callbacks during rspec testing?
我有回调名称 authenticatie_admin_user。如果当前用户电子邮件 ID 为“adminuser@admin.com”
,它将 return 为真
def authenticate_admin_user!
if Current.user.blank? || Current.user.email != "adminuser@admin.com"
redirect_to courses_path, danger: "Admin login required"
end
end
此回调在我调用新操作时运行。
在测试新操作时,我必须设置 Current.user 值
describe "GET /new" do
describe "Create course with admin login"
it "will not renders a new course page" do
get new_course_url
expect(response).to_not be_successful
end
it "will render a new course page" do
get new_course_url
expect(response).to be_successful
end
end
如何设置Current.user的值?
PS:这里的 Current 是一个 ActiveSupport::Currentattributes 模型,user 是 Current class.
里面的一个属性
尝试存根用户对象:
let(:user) { double(:user, email: 'adminuser@admin.com') }
before do
allow(Current).to receive(:user).and_return(user)
end
或者只是
before do
allow(Current).to receive(:user).and_return(nil)
end
我有回调名称 authenticatie_admin_user。如果当前用户电子邮件 ID 为“adminuser@admin.com”
,它将 return 为真def authenticate_admin_user!
if Current.user.blank? || Current.user.email != "adminuser@admin.com"
redirect_to courses_path, danger: "Admin login required"
end
end
此回调在我调用新操作时运行。 在测试新操作时,我必须设置 Current.user 值
describe "GET /new" do
describe "Create course with admin login"
it "will not renders a new course page" do
get new_course_url
expect(response).to_not be_successful
end
it "will render a new course page" do
get new_course_url
expect(response).to be_successful
end
end
如何设置Current.user的值? PS:这里的 Current 是一个 ActiveSupport::Currentattributes 模型,user 是 Current class.
里面的一个属性尝试存根用户对象:
let(:user) { double(:user, email: 'adminuser@admin.com') }
before do
allow(Current).to receive(:user).and_return(user)
end
或者只是
before do
allow(Current).to receive(:user).and_return(nil)
end