结果本来应该改1,但不是吗?

时间:2013-06-11 15:49:08

标签: ruby-on-rails-3 rspec

虽然在控制台和浏览器中都能正常工作,但我无法获得此通行证。谢谢你的帮助!

这是我的规格:

  before(:each) do
    @attr = {name: "Example Class"}
    @create = post :create, {user_id: @user.id, student_group: @attr}
  end

  it "should create a user" do  
    lambda do
      @create
    end.should change {@user.student_groups.count}.by(1)
  end

我在控制器中的newcreate操作:

def new
  @student_group  = @user.student_groups.new
  @title = "Create a new class"
end

...

def create
  @student_group = @user.student_groups.create(params[:student_group])
  if @student_group.save
    # for now redirect to 
  redirect_to classes_path, flash: { success: "#{@student_group.name} created! Next, add some students" }
    # redirect_to new_student_group_student_path
  else
    @title = "Create a new class"
    flash.now[:error] = "Something's gone wrong.  Please try again!"
    render 'new' 
  end  
end

和错误:

1) StudentGroupsController POST 'create' success should create a user
 Failure/Error: lambda do
   result should have been changed by 1, but was changed by 0
 # ./spec/controllers/student_groups_controller_spec.rb:113:in `block (4 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:2)

你的lambda不包含可执行代码,我认为移动创建代码到这个lambda将解决问题。

it "should create a user" do  
  lambda do
    post :create, {user_id: @user.id, student_group: @attr}
  end.should change {@user.student_groups.count}.by(1)
end