Rails登录测试 - 夹具未更新?

时间:2016-08-14 12:49:02

标签: ruby-on-rails ruby testing web-applications tdd

在我的应用程序用户登录后获得+1经验。我正在尝试编写一个测试,检查是否已经添加了体验。

用户模型:

def add_experience(how)
    update_attribute(:experience, experience + how)
 end

会话助手:

def log_in(user)
    session[:user_id] = user.id
    user.add_experience(1)
 end

和会话控制器

def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      flash[:success] = "Success!"
      log_in user
      params[:session][:remember_me] == '1' ? remember(user) : forget(user)
      redirect_to root_path
    else
      flash.now[:danger] = 'Ups!' 
      render 'static_pages/home'
    end
  end

登录后实际添加了经验值,但测试出错误:

"@user.experience" didn't change by 1.
Expected: 1
  Actual: 0

测试登录:

def setup
    @user = users(:example)
 end

test "example login test" do
    get root_path
    assert_difference '@user.experience', 1 do
    post login_path, params: { session: { email:    @user.email,
                                          password: 'password' } }
    end
end

灯具:

example:
  name: Example name
  email: ex@example.com
  password_digest: <%= User.digest('password') %>
  experience: 0

0 个答案:

没有答案