More Related Content Similar to Testing with mock object (20) Testing with mock object8. def test_create
post :create, :user =>
{:name => 'test'}
end
def test_update
put :update, :user =>
{:name => 'new'}
end 23. def test_xxx
@post.expects(:digg)
end
#<Mock:post>.digg -
expected calls: 1, actual
calls: 0 29. @post.expects(:method)
.at_least(0)
@post.expects(:method)
.at_least(0)
.returns(:result)
@post.stubs(:method)
@post.stubs(:method =>
:result) 32. def test_create
post :create, :user =>
{:name => 'test'}
end
add_column :first_name
add_column :last_name
remove_column :name 33. def test_should_create_user
Post.expects(:new)
.returns(@post)
@post.expects(:save)
.returns(true)
post :create, :user => {}
assert_redirect_to
user_path(@user)
end 35. # teachers_students.yml
one:
teacher_id: 1
不够直观 student_id: 1
two:
teacher_id: 1
浪费时间 student_id: 2
three:
teacher_id: 2
student_id: 3 36. def setup
@user = User.new(:name=>'test')
@post1 =
Post.new(:title=>'post1')
end
def test_should_show_post
User.expects(:find)
.returns(@user)
@user.posts.expects(:find)
.returns(@post1)
get :show, :id=>1, :user_id=>1
assert_response :success
end 38. it “should create a new user” do
User.should_receive(:new)
.and_return(@user)
User.stub!(:save)
.and_return(true)
post :create, :user => {}
response.should
redirect_to(user_path(@user))
end