|
|
|
@@ -0,0 +1,189 @@ |
|
|
|
require 'spec_helper'
|
|
|
|
describe PollController do
|
|
|
|
|
|
|
|
|
|
|
|
# 创建测试数据
|
|
|
|
before do
|
|
|
|
@poll=create(:poll)
|
|
|
|
@poll_php=create(:poll,polls_name:'java',polls_group_id:'156')
|
|
|
|
p @poll.errors
|
|
|
|
p @poll_php.errors
|
|
|
|
end
|
|
|
|
# index动作的测试
|
|
|
|
# 错误分析:index测试,得到的polls为nil把,因为没传递任何参数,在index action中@course为nil,
|
|
|
|
# 应该传参数poll_type = 'Course',参考before_filter: find_container,is_member_of_course中的代码
|
|
|
|
describe "GET index" do
|
|
|
|
it "assigns all polls as @polls" do
|
|
|
|
# @poll = Poll.create! valid_attributes
|
|
|
|
# get请求,index动作,传入的参数
|
|
|
|
#get :index, {:id => @poll.to_param}, {}
|
|
|
|
get :index,:poll => { :polls_type => @poll.polls_type,:polls_group_id => @poll.polls_group_id }
|
|
|
|
# 返回的实际值和期望值是否相等,用eq表示相等,不用“==”
|
|
|
|
# 控制器实例化的变量通过:assigns(:variable_name)获取
|
|
|
|
expect(assigns(:polls)).to eq([@poll])
|
|
|
|
end
|
|
|
|
# 返回指定的模板
|
|
|
|
it"renders the index view"do
|
|
|
|
get :index
|
|
|
|
#控制其动作的返回浏览器的结果可以使用respond获取,此处返回的结果是渲染index模板
|
|
|
|
expect(response).to render_template :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 对show动作的测试
|
|
|
|
describe "GET show" do
|
|
|
|
it "show poll as @poll" do
|
|
|
|
get :show,id:@poll
|
|
|
|
expect(assigns( :poll)).to eq @poll
|
|
|
|
end
|
|
|
|
it"render the show template"do
|
|
|
|
@poll=create(:poll)
|
|
|
|
get :show, id: @poll
|
|
|
|
expect(response).to render_template :show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
#对new动作的测试
|
|
|
|
describe "GET new" do
|
|
|
|
# it "assigns a new poll as @poll" do
|
|
|
|
# get :new
|
|
|
|
# expect(assigns( :poll)).to be_a_new( Poll)
|
|
|
|
# end
|
|
|
|
# it " renders the :new template" do
|
|
|
|
# get :new
|
|
|
|
# expect(response).to render_template :new
|
|
|
|
# end
|
|
|
|
it "assigns a new poll as @poll" do
|
|
|
|
get :new, {}, valid_session
|
|
|
|
assigns(:poll).should be_a_new(Poll)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
#对edit动作的测试
|
|
|
|
describe 'GET edit' do
|
|
|
|
it "assigns the requested poll to @poll" do
|
|
|
|
# @poll = create(:poll)
|
|
|
|
get :edit, id: @poll
|
|
|
|
expect(assigns(:poll)).to eq @poll
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders the :edit template" do
|
|
|
|
#@poll = create(:poll)
|
|
|
|
get :edit, id: @poll
|
|
|
|
expect(response).to render :layout => 'base_courses'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
#对update动作的测试
|
|
|
|
describe "PATCH update" do
|
|
|
|
context "vaild attributes" do
|
|
|
|
# 更新动作:输入合法,更新成功的情况
|
|
|
|
it"locates the requested @poll"do
|
|
|
|
# attributes_for 方法生成的不是 Ruby 对象,而是一个由属性组成的 Hash,用来模拟浏览器和服务器之间的操作
|
|
|
|
put :update, id: @poll.id, poll: attributes_for( :poll)
|
|
|
|
expect(assigns( :poll)).to eq(@poll)
|
|
|
|
end
|
|
|
|
|
|
|
|
#before{@poll = Poll.create! valid_attributes}
|
|
|
|
it "assigns the requested poll as @poll" do
|
|
|
|
# put :update ,{:id => @poll.to_param,:poll => valid_attributes}, valid_session
|
|
|
|
# expect(assigns(:poll)).to eq @poll
|
|
|
|
put :update,id:@poll,poll: attributes_for(:poll,polls_name:"china",polls_group_id:"1234")
|
|
|
|
@poll.reload
|
|
|
|
expect(@poll.polls_name).to eq("china")
|
|
|
|
expect(@poll.polls_description).to eq("1234")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to the poll" do
|
|
|
|
put :update,id:@poll,poll:attributes_for(:poll)
|
|
|
|
expect(response).to redirect_to @poll
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# 非法信息,更新失败的情况
|
|
|
|
context "invalid attributes" do
|
|
|
|
it "update poll failure" do
|
|
|
|
put :update,id:@poll,poll:attributes_for(:poll,polls_name:nil,polls_description:"test")
|
|
|
|
@poll.reload
|
|
|
|
expect(@poll.polls_name).to eq("php")
|
|
|
|
expect(@poll.polls_description).to_not eq("test")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders the 'edit' template" do
|
|
|
|
# poll = Poll.create! valid_attributes
|
|
|
|
# # Trigger the behavior that occurs when invalid params are submitted
|
|
|
|
# Poll.any_instance.stub(:save).and_return(false)
|
|
|
|
# put :update, {:id => poll.to_param, :poll => { "polls_name" => "invalid value" }}, valid_session
|
|
|
|
# response.should render_template("edit")
|
|
|
|
put :update,id:@poll,poll:attributes_for(:invalid_contact)
|
|
|
|
expect(response).to render_template :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# describe "PUT update" do
|
|
|
|
# context "with valid params" do
|
|
|
|
# it "updates the requested poll" do
|
|
|
|
# poll = Poll.create! valid_attributes
|
|
|
|
# Poll.any_instance.should_receive(:update).with({ "polls_name" => "MyString" })
|
|
|
|
# put :update, {:id => poll.to_param, :poll => { "polls_name" => "MyString" }}, valid_session
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it "assigns the requested poll as @poll" do
|
|
|
|
# @poll = Poll.create! valid_attributes
|
|
|
|
# put :update, {:id => poll.to_param, :poll => valid_attributes}, valid_session
|
|
|
|
# assigns(:poll).should eq(@poll)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it "redirects to the poll" do
|
|
|
|
# poll = Poll.create! valid_attributes
|
|
|
|
# put :update, {:id => poll.to_param, :poll => valid_attributes}, valid_session
|
|
|
|
# response.should redirect_to(poll)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# context "with invalid params" do
|
|
|
|
# it "assigns the poll as @poll" do
|
|
|
|
# poll = Poll.create! valid_attributes
|
|
|
|
# Poll.any_instance.stub(:save).and_return(false)
|
|
|
|
# put :update, {:id => poll.to_param, :poll => { "polls_name" => "invalid value" }}, valid_session
|
|
|
|
# assigns(:poll).should eq(poll)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it "re-renders the 'edit' template" do
|
|
|
|
# poll = Poll.create! valid_attributes
|
|
|
|
# Poll.any_instance.stub(:save).and_return(false)
|
|
|
|
# put :update, {:id => poll.to_param, :poll => { "polls_name" => "invalid value" }}, valid_session
|
|
|
|
# response.should render_template("edit")
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# #对create动作的测试
|
|
|
|
# describe "POST create" do
|
|
|
|
# it"should redirect to index with a notice on successful save"do
|
|
|
|
# Poll.any_instance.stubs(:valid?).returns(true)
|
|
|
|
# post 'create'
|
|
|
|
# response.should redirect_to(poll_path)
|
|
|
|
# end
|
|
|
|
# it"should re-render new template on failed save"do
|
|
|
|
# Poll.any_instance.stubs(:valid).returns(false)
|
|
|
|
# response.should render_template('new')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
#对delete动作的测试
|
|
|
|
describe "DELETE destroy" do
|
|
|
|
it "destroy the requested poll" do
|
|
|
|
poll = Poll.create! valid_attributes
|
|
|
|
# delete请求,destroy动作,参数(可选),session(可选)
|
|
|
|
expect{ delete :destroy,{:id=>poll.to_param},valid_session }.to change(Poll,:count).by(-1)
|
|
|
|
end
|
|
|
|
it "redirects to the polls list" do
|
|
|
|
poll = Poll.create! valid_attributes
|
|
|
|
# delete :destroy,id:@poll
|
|
|
|
# expect(response).to redirect_to poll_path
|
|
|
|
delete :destroy, {:id => poll.to_param}, valid_session
|
|
|
|
response.should redirect_to(poll_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end |