所以我正在测试我在RSPEC中的一个控制器,我正在尝试测试整个更新的恶作剧。这就是我在RSPEC中所拥有的。
describe 'PUT update' do
before :each do
@user = Factory(:user,name: "Lawrence Smith")
end
context "valid attributes" do
it "located the requested @user" do
put :update, :id => @user, user: Factory.attributes_for(:user)
assigns(:user).should eq(@user)
end
it "changes @user's attributes" do
put :update, :id=> @user.id, user: Factory.attributes_for(:user, name: "Larry Smith")
@user.reload
@user.name.should eq("Larry Smith")
end
end
这就是我在控制器中的含义
def update
#update and save
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
sign_in @user
format.json{render :json => @user ,:status => :created, :location => @user }
else
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
端 显然,@ user.reload并没有真正刷新ActiveRecord,因为我的错误显示该名称根本没有被更改。知道什么是错的吗?
答案 0 :(得分:0)
我不知道如何在get或post参数中传递对象。无论如何尝试这样 描述'PUT更新'做 之前:每个都做 @user = Factory(:用户,名称:“Lawrence Smith”) 端
context "valid attributes" do
it "located the requested @user" do
put :update, :id => @user, user: Factory.attributes_for(:user)
assigns(:user).should eq(@user)
end
it "changes @user's attributes" do
put :update, :id=> @user.id, :user_name => Factory.attributes_for(:user, name: "Larry Smith").name
@user.reload
@user.name.should eq("Larry Smith")
end
end
这在控制器中
def update
#update and save
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(:name => params[:user_name])
sign_in @user
format.json{render :json => @user ,:status => :created, :location => @user }
else
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
我认为你得到了我在这里解释的内容。
由于