比方说有一个字符串" world ".此字符串仅在前端和末尾有空白.是trim()快replace()吗?
我使用了替换一次,我的导师说不要使用它,因为trim()可能更快.
如果不是,有什么优势trim()比replace()?
我有一个方法可以更新人的属性,ActiveRecord::RecordNotFound如果找不到人的话它还有救。方法是:
def update
@people= People.find(params[:id])
if @people.update(people_params)
render json: { success: 'Success' }
else
render :edit
end
rescue ActiveRecord::RecordNotFound => e
render json: { error: 'Failed') }
end
Run Code Online (Sandbox Code Playgroud)
我想测试一下找不到记录时的情况,这是我现在的测试:
let(:people) { create(:people) }
let(:people_id) { people.id }
let(:user) { people}
# Other tests...
context 'when person not found' do
let(:exception) { ActiveRecord::RecordNotFound }
# What should I write so that I can let the record not been found?
before { allow(People).to receive(:find).and_raise(exception) }
it 'responds with json containing …Run Code Online (Sandbox Code Playgroud)