未定义的方法`scan&#39;对于#<hash:0x00000004effe80> </hash:0x00000004effe80>

时间:2014-08-14 10:39:53

标签: ruby-on-rails ruby ruby-on-rails-4 rspec

当我尝试运行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

1 个答案:

答案 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