小编Mei*_*lan的帖子

String.trim()比String.replace()更快吗?

比方说有一个字符串" world ".此字符串仅在前端和末尾有空白.是trim()replace()吗?

我使用了替换一次,我的导师说不要使用它,因为trim()可能更快.

如果不是,有什么优势trim()replace()

java

5
推荐指数
1
解决办法
1569
查看次数

rspec - 如何测试 ActiveRecord::RecordNotFound?

我有一个方法可以更新人的属性,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)

ruby unit-testing rspec ruby-on-rails

2
推荐指数
1
解决办法
1908
查看次数

标签 统计

java ×1

rspec ×1

ruby ×1

ruby-on-rails ×1

unit-testing ×1