与水豚错误重定向

时间:2013-07-13 18:09:23

标签: ruby testing rspec capybara

我在上一个问题上遇到了问题,我帮了,但是现在我接受了新的问题。 我正在与rspec和capybara进行集成测试。

这是我的profiles_controllers.rb:

 before_filter :authenticate_user!

  def update
    @profile = current_user.profile
    if @profile.update_attributes(params[:profile])
        flash[:success] = "Профиль обновлен!"
        redirect_to user_path(current_user)
    else
      render 'edit'
    end
  end

这是我的测试文件:

describe "ProfilePages" do

subject { page }

describe "edit" do
let(:user) { FactoryGirl.create(:user) }
let(:profile) { FactoryGirl.create(:profile, user: user) }

before do
  login user
  visit edit_profile_path(profile)
end

it { should have_selector('h2', text: 'Заполните информацию о себе') }

describe "change information" do
  let(:new_city)  { "Ulan-Bator" }
  let(:new_phone) { 1232442 }
  let(:new_gamelevel) { "M2" }
  let(:new_aboutme)   { "nfsfsdfds" }
  let(:submit) { "Сохранить" }
  before do
    fill_in "Город",             with: new_city
    fill_in "Телефон",           with: new_phone
    select new_gamelevel,        from: "Уровень игры"
    fill_in "О себе",            with: new_aboutme
    click_button submit
  end
  specify { profile.reload.city.should  == new_city }
  specify { profile.reload.phone.should == new_phone }
  specify { profile.reload.gamelevel.should == new_gamelevel }
  specify { profile.reload.aboutme.should == new_aboutme }
end

describe "submitting to the update action" do
  before  { put profile_path(profile) }
  specify { response.should redirect_to(user_path(user)) }
end
end
end

我有错误:

失败/错误:指定{response.should redirect_to(user_path(user))}        预期回复是重定向到http://www.example.com/users/1,但重定向到http://www.example.com/users/sign_in

我使用Devise并在spec / support中拥有登录助手:

def login(user)
 page.driver.post user_session_path, 'user[email]' => user.email, 'user[password]'       =>    user.password
end

spec_helper.rb中的config.include Devise::TestHelpers, :type => :controller

我尝试过使用warden helper login_as,但是有相同的错误。我怎么理解它不开始会话,我是对的?

1 个答案:

答案 0 :(得分:0)

这与您的应用代码无关,而是与测试代码无关。

response对象用于控制器集成测试,Capybara中没有这样的对象。

通常,您可以使用page对象来检查响应信息。对于路径检查,更好的方法是current_pathcurrent_url

所以你的代码将起作用:

current_path.should be(user_path(user))
相关问题