我试图通过将哈希表放入let中并仅调用let来DRY化我的代码,但似乎无效。我得到以下错误:
syntax error, unexpected =>, expecting '}' (SyntaxError) "email" => user.email,
syntax error, unexpected =>, expecting :: or '[' or '.' ...l" => user.email, "password" => user.password }
这是我的测试:
describe '#create' do
let(:user) { create(:user) }
let(:user_params) { "email" => user.email, "password" => user.password }
before(:each) { User.stub(:find_by_email).and_return(user) }
it "should send find message to user model" do
User.should_receive(:find_by_email)
post :create, locale: "es", user: { user_params }
end
it "should set user to @user" do
post :create, locale: "es", user: { "email" => user.email, "password" => user.password }
assigns(:user).should eq(user)
end
end
这是我的代码:
def create
@user = User.find_by_email(params[:email])
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect_to root_url
else
render 'login'
end
end