作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 rspec 来测试使用已声明 attr_accessible 的模型的 Controller 。我不想测试 attr_accesible 是否正常工作(我的模型规范就是这样做的),但我确实想确保我的 Controller 没有执行批量分配。
具体来说,我有一个这样的模型:
class Post < ActiveRecord::Base
attr_accessible :body
validates :body, :presence => true,
validates :user, :presence => true
belongs_to :user
end
def create
# this line keeps the rspec test mock happy
@post = Post.new(params[:post].merge(:user => current_user))
# Below are the correct lines of code for runtime for attr_accessible
# @post = Post.new(params[:post])
# @post.user = current_user
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created post as @post" do
Post.stub(:new).
with({'body' => 'hi there', 'user' => @user}) { mock_post(:save => true) }
post :create, :post => {'body' => 'hi there'}
assigns(:post).should be(mock_post)
end
end
end
最佳答案
检查批量分配。你需要复制它。所以在请求中发送错误的用户 ID
post :create, :post => {'body' => 'hi there', 'user_id' => '2'}
assigns(:post)user_id.should be(current_user.id)
关于ruby-on-rails-3 - 如何编写不考虑质量分配的 rspec Controller 规范 model_mock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5480936/
我正在使用 rspec 来测试使用已声明 attr_accessible 的模型的 Controller 。我不想测试 attr_accesible 是否正常工作(我的模型规范就是这样做的),但我确实
我是一名优秀的程序员,十分优秀!