我开始通过黄瓜研究BDD。 (使用Rails-3,gem'cucumber-rails')
我想在成功登录后重定向到用户个人资料页面(/ users / id)
我在控制器中定义为(redirect_to user_path(@user))和我在黄瓜中定义的相同的东西(page.current_path.should == user_path(@user))
在我的step_definition
中Given /^a user visits the signin page$/ do
visit signin_path
end
When /^he log in as "(.*)\/(.*)"$/ do |email, password|
@email = email
fill_in "email", with: email
fill_in "password", with: password
click_button "Login"
end
Then /^he should see a signin link$/ do
page.should have_link('Sign in', href: signin_path)
end
Then /^he should see his profile page$/ do
@user = User.where("email = ?", @email)
page.current_path.should == user_path(@user)
end
在我的控制器中
class SessionsController < ApplicationController
def create
@user = User.find_by_email(params[:session][:email].downcase)
if @user && @user.authenticate(params[:session][:password])
sign_in @user
redirect_to user_path(@user)
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
end
现在运行黄瓜时,我收到了这个错误:
expected: "/users/%23%3CActiveRecord::Relation:0x000000062fbec0%3E"
got: "/users/4" (using ==) (RSpec::Expectations::ExpectationNotMetError)
在我的features / support / env.rb中:
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails'
require 'rspec/expectations'
请回答我做错的地方。
答案 0 :(得分:0)
步骤定义中的此代码是问题:
Then /^he should see his profile page$/ do
@user = User.where("email = ?", @email)
page.current_path.should == user_path(@user)
end
User.where
将始终返回关系,即使结果只是一条用户记录,因此错误消息的ActiveRecord::Relation
部分也是如此。
如果您确定只返回一个用户,则可以将其换成User.find_by_email(@email)
或User.where(:email => @email).first
,以获取单个用户实例。