首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >黄瓜路由问题中的RSpec::Expectations::ExpectationNotMetError

黄瓜路由问题中的RSpec::Expectations::ExpectationNotMetError
EN

Stack Overflow用户
提问于 2013-02-28 15:42:22
回答 1查看 2.7K关注 0票数 0

我通过cucumber开始了BDD的工作。(使用Rails-3,gem 'cucumber-rails')

我希望在成功登录时重定向到用户配置文件页面(/users/id)。

我在控制器中定义为(redirect_to user_path(@user)),在cucumber中定义为(page.current_path.should == user_path(@user))

在我的step_definition

代码语言:javascript
复制
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

在我的控制器中

代码语言:javascript
复制
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时,我得到了这个错误:

代码语言:javascript
复制
expected: "/users/%23%3CActiveRecord::Relation:0x000000062fbec0%3E"
       got: "/users/4" (using ==) (RSpec::Expectations::ExpectationNotMetError)

在my features/support/env.rb中:

代码语言:javascript
复制
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails'
require 'rspec/expectations'

请回答我哪里做错了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-28 16:45:45

步骤定义中的代码就是问题所在:

代码语言:javascript
复制
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之类的内容,以获得单个用户实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15130074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档