def test_should_create_post_successfully
user = FactoryGirl.create(:user_valid)
category = FactoryGirl.create(:category_valid)
#@request.session[:user_id] = user.id
assert_difference('Post.count') do
article = FactoryGirl.create(:post_valid)
post :create, {:post=>{:category_id=>category.id,
:title=>article.title,
:slug=>article.slug,
:summary=>article.summary,
:content=>article.content}},
{:user_id => user.id}
end
#assert_redirected_to admin_posts_path(assigns(:posts))
end
上面是我的功能测试代码,针对 postscontroller 的 create。 controller 中的代码是这样的。
def create
@category = Category.find(params[:post][:category_id])
params[:post].delete(:category_id)
@post = @category.posts.build(params[:post])
@post.user = current_user
@post.tag_ids = params[:tag_ids]
if @post.save
flash[:notice] = "post was created successfully"
redirect_to admin_posts_path
else
flash.now[:notice] = "create post failed"
render :new
end
end
create 成功之后会跳转,我在 chrome 的 developer tools 下面看到的也是一个 302,然后才是 200. 但是我的测试却提示我测试失败。
test_should_create_post_successfully(Admin::PostsControllerTest) [test/functional/admin/posts_controller_test.rb:22]:
Expected response to be a <:redirect>, but was <200>