这是我正在测试的控制器中的操作。
def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
login_user!(user)
else
flash[:danger] = "Something was wrong with your username or password."
render :new
end
end
这是我对此行动的规范
describe "POST create" do
let!(:bob) { Fabricate(:user, email: "bob@bob.com") }
it "sets user to the user with a matching email" do
post :create, email: "bob@bob.com"
expect(assigns(:user)).to eq(bob)
end
end
当我运行时,它说
expected: #<User id: 1, email: "bob@bob.com", password_digest: "$2a$04$e5M21As5A6HaGBhaFsfzE.9hw4mlfYDPQbOCB9VggAI...", full_name: "Sidney D'Amore MD", created_at: "2014-07-29 15:42:02", updated_at: "2014-07-29 15:42:02">
got: nil
我在这里缺少什么?在我看来,HTTP请求应该触发此行
user = User.find_by(email: params[:email])
并将user
分配给bob
答案 0 :(得分:4)
将您的操作更改为使用实例变量(@user
)。无法在rspec中测试局部变量赋值
def create
@user = User.find_by(email: params[:email])
if @user && @user.authenticate(params[:password])
login_user!(@user)
else
flash[:danger] = "Something was wrong with your username or password."
render :new
end
end