开发者

railstutorial.org - rspec failed but in browser everything is OK

开发者 https://www.devze.com 2023-03-22 06:00 出处:网络
I am trying to follow Rails Tutorial and I have a failed rspec but in browser the appworks as needed. That is when I am signed in (in browser) and go to \"http://localhost:3000/users/1/edit\" this pag

I am trying to follow Rails Tutorial and I have a failed rspec but in browser the app works as needed. That is when I am signed in (in browser) and go to "http://localhost:3000/users/1/edit" this page opens. All previous rspecs from the tutorial are passed.

I am using Rails '3.1.0.rc4' and working in Windows.

The s开发者_开发百科pec which fails (from users_controller_spec.rb):

describe "GET 'edit" do
    before(:each) do
      @user = Factory(:user)
      test_sign_in(@user)
    end

    it "should be successful" do
      get :edit, :id => @user
      response.should be_success
    end
end

code of test_sign_in from spec_helper.rb:

RSpec.configure do |config|  
  config.mock_with :rspec

  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true

  def test_sign_in(user)
    controller.sign_in(user)
  end
end

code of factories.rb

Factory.define :user do |user|
  user.name "Mikhail"
  user.email "mikleb@yandex.ru"
  user.password "123456789a"
  user.password_confirmation "123456789a"
end

text from console:

Failures:

  1) UsersController GET 'edit should be successful
     Failure/Error: response.should be_success
       expected success? to return true, got false
     # ./spec/controllers/users_controller_spec.rb:111:in `block (3 levels) in <
top (required)>'

UPD: After "get :edit, :id => @user" user is not signed in. But as I wrote in browser everything works as needed. How is that possible?

here is SessionHelper which is included in ApplicationControlelr:

module SessionsHelper
  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    self.current_user = user
  end

  def sign_out
    cookies.delete(:remember_token)
    self.current_user = nil
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @curent_user = user
  end

  def current_user
    @current_user ||= user_from_remember_token
  end

  def deny_access
    redirect_to signin_path, :notice => "Please sign in to access this page."
  end

  private
    def user_from_remember_token
      User.authenticate_with_salt(*remember_token)
    end

    def remember_token
      cookies.signed[:remember_token] || [nil, nil]
    end
end


Have you dumped response.body to see what Rspec thinks it's seeing? You may (I forget if this is still necessary) need to enable render_views as described in this question).

Is it possible the user is not really logged in and thus the site is redirecting from edit to the sign-in page or something similar?

Another caveat, I am pretty sure rspec only returns true for be_success if the HTTP code is a 2xx code (200 ok, 201 created, etc). So if your controller is redirecting (302, 304, etc) then rspec will not consider the request to have "been successful."

Try dropping in a puts response.body before your assertion, it may help you narrow down your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消