尝试为我已经编写的代码编写一些测试,以期使用测试驱动开发来扩展我的代码。
我有一个控制器,其索引操作调用“user_info”方法,它只是收集一些依赖于 Sorcery 的 current_user 变量的实例变量。例如:
def user_info
@current_A = current_user.a
@current_B = current_user.b
end
def index
user_info
// rest of the method goes here
end
我开始使用 rspec 编写一些测试,只是为了感受一下测试这个代码库。我的控制器规格非常基本,如下所示:
describe MyController do
describe "GET 'index'" do
get 'index'
response.should be_success
end
end
但是,当我尝试运行此规范时出现以下错误:NoMethodError: undefined method 'a' for false:FalseClass
首先,如何让我的规范识别 Sorcery 方法 current_user?而且,出于好奇,为什么 current_user 被标记为 FalseClass 的实例?如果它没有调用 Sorcery 方法(并且我没有在代码中的其他任何地方定义 current_user),它不应该显示为 nil 吗?