0

我知道我可以用它assigns来查看实例变量是否被分配给某个东西,但是我如何测试另一个属性是否已设置?

class UsersController < ApplicationController
  def new
     @user = User.new
     @user.name = "Tom Lehrer"
  end
end

describe UsersController do
  it "assigns a user" do
    get :new
    expect(assigns(:user)).to be_a_new(User)
  end

  it "sets the new users name" do
    # ??
  end
end
4

3 回答 3

2

你可以只测试assigns(:user)

expect(assigns(:user).name).to eq("Expected Name")
于 2013-07-04T01:22:58.430 回答
2

assigns(:user).name.should == 'Name here'

于 2013-07-04T14:40:13.493 回答
0

不是很明显吗?

User.any_instance.should_receive(:name).with("Tom Lehrer")
get :new 
于 2013-07-04T01:21:22.077 回答