我試圖通過將一個散列放入一個let中並乾脆調用let來幹掉我的代碼,但它似乎不起作用。我得到這些錯誤:在let中使用散列()
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
這就是完全它。謝謝! – freddyrangel