page.should have_content('以subscribem@example.com&#39格式登录;)

时间:2013-02-17 20:30:22

标签: ruby-on-rails-3 rspec warden

我正在按照 Ryan Bigg的多租户一书中的步骤我遇到了这个错误,我找不到我做错了什么。

错误:

  1) Accounts Creating an account
     Failure/Error: page.should have_content('Signed in as subscribem@example.com')
       expected there to be text "Signed in as subscribem@example.com" in "Your account has been successfully created. Account Sign Up"
     # ./spec/features/accounts/sign_up_spec.rb:13:in `block (2 levels) in <top (required)>'

sign_up_spec.rb

require 'spec_helper'
feature 'Accounts' do 
  scenario "Creating an account" do
    visit subscribem.root_url
    click_link 'Account Sign Up'
    fill_in 'Name', :with => "Test"
    fill_in 'Email', :with => 'subscribem@example.com'
    password_field_id = 'account_owner_attributes_password'
    fill_in 'Password confirmation', :with => 'password'
    click_button 'Create Account'
    success_message = 'Your account has been successfully created'
    page.should have_content(success_message)
    page.should have_content('Signed in as subscribem@example.com')
  end
end                                                                                         

帐户模型

module Subscribem
  class Account < ActiveRecord::Base
    attr_accessible :name, :owner_attributes

    belongs_to :owner, :class_name => 'Subscribem::User'
    accepts_nested_attributes_for :owner
  end
end

用户模型

module Subscribem
  class User < ActiveRecord::Base
    attr_accessible :email, :password, :password_confirmation
    has_secure_password

  end
end

应用程序控制器

module Subscribem
  class ApplicationController < ActionController::Base

    def current_user
      if user_signed_in?
        Subscribem::Account.find(env['warden'].user(:scope => :account))
      end
    end

    helper_method :current_account

    def current_user
      if user_signed_in?
        Subscribem::User.find(env['warden'].user(:scope => :user))
      end
    end

    helper_method :current_user

    def user_signed_in?
      env['warden'].authenticated?(:user)
    end

    helper_method :user_signed_in?

  end
end  

帐户控制器

require_dependency "subscribem/application_controller"

module Subscribem
  class AccountsController < ApplicationController

    def new
      @account = Subscribem::Account.new
      @account.build_owner
    end

    def create
      account = Account.create(params[:account])
      env['warden'].set_user(account.owner.id, :scope => :user)
      env['warden'].set_user( account.id, :scope => :account)
      flash[:success] = "Your account has been successfully created."
      redirect_to subscribem.root_url
    end

  end
end

application.html.erb

<body>                                                                             
<% flash.each do |k,v| %>
<div class='flash <%= k %> '><%= v %></div>
<% end %>

<% if current_user %>
Signed in as <%= current_user.email %>
<% end %>

<%= yield %>

</body>

2 个答案:

答案 0 :(得分:0)

也许错误在视图中(subcribem / accounts / show.html。[template_engine])?似乎只打印出flash消息。

答案 1 :(得分:0)

我遇到了同样的问题,并以不同的期望解决了这个问题:

require 'rails_helper'

feature 'Accounts' do
  scenario 'creating an account' do
    visit subscribem.root_path
    click_link 'Account Sign Up'
    fill_in 'Name', with: 'Test'
    click_button 'Create Account'
    success_message = 'Your Account has been successfully created.'
    expect(find('.flash.success')).to have_content(success_message)
  end
end

解决方案是找到成功消息的flash消息而不是页面。 希望这有助于其他人。