当我尝试运行specs
时,我收到以下错误。
Failure/Error: user = User.find({id: 2856, authorization: token});
NoMethodError:
undefined method `scan' for #<Hash:0x00000004effe80>
以下是我的spec
。
#Spec for user find and current user
context "when user exists" do
it "should find a user" do
user = User.find({id: 2856, authorization: token});
expect(user.name).to eq("test")
end
end
答案 0 :(得分:2)
find
方法仅在最后一个Rails版本中按id
进行搜索,所以相反:
context "when user exists" do
it "should find a user" do
user = User.find({id: 2856, authorization: token});
expect(user.name).to eq("test")
end
end
使用:
context "when user exists" do
it "should find a user" do
user = User.where(id: 2856, authorization: token).first
expect(user.name).to eq("test")
end
end
或更好:
context "when user exists" do
it "should find a user" do
user = User.where(authorization: token).find(2856)
expect(user.name).to eq("test")
end
end