如何为“where”行编写测试

时间:2013-03-20 10:10:52

标签: ruby-on-rails ruby mongodb omniauth minitest

我想编写与控制器测试相关的密码更新测试。我在控制器的第一行找到了经过身份验证的人员。如何编写与此相关的测试。我无法提出任何想法。

ChangePasswordsController

def update
  person = Person.where(_id: session[:user_id]).first
  identity = Identity.where(_id: person.user_id).first
  unless params[:new_password] != params[:new_password_confirmation]
    identity.password = params[:new_password].to_s
    identity.password_confirmation = params[:new_password].to_s
    identity.save
    redirect_to root_url, :notice => "Password has been changed." + person.user_id
  else
    redirect_to :back, :alert => "Password & password confirmation are not match" 
  end
end

ChangePasswordsController测试

describe ChangePasswordsController do

  setup do
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:identity] 
    @auth=request.env["omniauth.auth"]
  end

  it "should have edit action" do
    get :edit
    assert_response :success
  end

  it "should find person" do
     ... 
  end

  it "should find identity" do
     ... 
  end

end

1 个答案:

答案 0 :(得分:0)

我认为你不应该写一个测试是否在哪里找到一个人。在这种情况下,您最好只检查update方法是否使用给定参数调用Person上的find方法。使用这样的东西:

Person.should_receive(:find).with(id: <the userid you've setup for the test>) {[Person.new]}

它应该能够先检查但我不记得了,与链接存储有关。

相关问题