我通过cucumber开始了BDD的工作。(使用Rails-3,gem 'cucumber-rails')
我希望在成功登录时重定向到用户配置文件页面(/users/id)。
我在控制器中定义为(redirect_to user_path(@user)),在cucumber中定义为(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现在,当运行cucumber时,我得到了这个错误:
expected: "/users/%23%3CActiveRecord::Relation:0x000000062fbec0%3E"
got: "/users/4" (using ==) (RSpec::Expectations::ExpectationNotMetError)在my features/support/env.rb中:
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails'
require 'rspec/expectations'请回答我哪里做错了。
发布于 2013-02-28 16:45:45
步骤定义中的代码就是问题所在:
Then /^he should see his profile page$/ do
@user = User.where("email = ?", @email)
page.current_path.should == user_path(@user)
endUser.where将始终返回一个关系,即使结果只有一条用户记录,因此错误消息中的ActiveRecord::Relation部分也是如此。
如果您可以确保只返回一个用户,则可以将其替换为User.find_by_email(@email)或User.where(:email => @email).first之类的内容,以获得单个用户实例。
https://stackoverflow.com/questions/15130074
复制相似问题