R规格回形针:attr_accessible 损坏?
我在让我的 rspec 控制器测试通过 Rspec 中的 attr_accessible 时遇到问题...但不是从控制台。
post :create, :banner => valid_attributes
失败,但是
Banner.create!(valid_attributes) is accepted.
如果我从横幅模型中取出 attr_accessible ,或者取出 validates_attachment_presence :bannerimage ,它就会起作用。尝试将bannerimage_attributes和四个回形针生成的:bannerimage列添加到我的attr_accessible中-没有乐趣。尝试取出其他回形针验证器(内容类型、大小) - 仍然没有乐趣。非常感谢任何建议 - 我完全不知所措。
相关代码在这里:
RSPEC测试的相关位:
def valid_attributes
demo_image = File.open(File.join(Rails.root, "spec", "samples", "laptop1.jpg"))
{
:name => 'Test Spec Banner',
:bannerimage => demo_image
}
end
describe "POST create" do
describe "with valid params" do
it "creates a new Banner" do
expect {
post :create, :banner => valid_attributes
}.to change(Banner, :count).by(1)
end
end
型号:
class Banner < ActiveRecord::Base
attr_accessible :name, :url, :bannerimage
has_attached_file :bannerimage, :styles => { :full => "960x", :thumb => "100x" }
validates_attachment_content_type :bannerimage, :content_type => [ 'image/jpg', 'image/jpeg', 'image/gif', 'image/png'], :message => 'file must be a gif, jpeg or png image'
validates_attachment_size :bannerimage, :less_than => 3.megabytes
validates_presence_of :name
validates_attachment_presence :bannerimage
validates_uniqueness_of :name
has_many :pages, :dependent => :nullify
def to_s
name
end
end
编辑: 横幅可以通过网站创建。
相关控制器代码如下。没有之前/之后的调用,只是一个标准的宁静创建。
def create
@banner = Banner.new(params[:banner])
if @banner.save
redirect_to admin_banner_url(@banner), notice: 'Banner was successfully created.'
else
render action: "new"
end
end
I'm having a problem getting my rspec controller tests to pass with attr_accessible in Rspec...but not from the console.
post :create, :banner => valid_attributes
fails, but
Banner.create!(valid_attributes) is accepted.
If I take out attr_accessible from the banners model, or I take out the validates_attachment_presence :bannerimage, it works. Have tried adding bannerimage_attributes and the four paperclip-generated :bannerimage columns to my attr_accessible- no joy. Tried taking out other paperclip validators (content type, size) - still no joy. Any suggestions very appreciated - I'm completely at a loss.
The relevant code is here:
Relevant bits of RSPEC test:
def valid_attributes
demo_image = File.open(File.join(Rails.root, "spec", "samples", "laptop1.jpg"))
{
:name => 'Test Spec Banner',
:bannerimage => demo_image
}
end
describe "POST create" do
describe "with valid params" do
it "creates a new Banner" do
expect {
post :create, :banner => valid_attributes
}.to change(Banner, :count).by(1)
end
end
Model:
class Banner < ActiveRecord::Base
attr_accessible :name, :url, :bannerimage
has_attached_file :bannerimage, :styles => { :full => "960x", :thumb => "100x" }
validates_attachment_content_type :bannerimage, :content_type => [ 'image/jpg', 'image/jpeg', 'image/gif', 'image/png'], :message => 'file must be a gif, jpeg or png image'
validates_attachment_size :bannerimage, :less_than => 3.megabytes
validates_presence_of :name
validates_attachment_presence :bannerimage
validates_uniqueness_of :name
has_many :pages, :dependent => :nullify
def to_s
name
end
end
Edit:
Banner can be created through site.
Relevant controller code below. No before/after calls, just a standard restful create.
def create
@banner = Banner.new(params[:banner])
if @banner.save
redirect_to admin_banner_url(@banner), notice: 'Banner was successfully created.'
else
render action: "new"
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码似乎没有任何可能导致此问题的明显问题。您应该注意的一件事是,您对
name
有唯一性约束,但valid_attributes
始终返回相同的名称。这不是你的问题所在,我只是提到它作为需要记住的事情。我只能为您提供有关如何自行尝试和调试的建议。您可以通过
assigns(:banner)
访问规范中新创建的横幅。因此,您可以执行以下操作:这会将横幅的错误转储到运行测试的控制台。如果横幅未保存,则意味着它无效,因此首先要做的就是检查生成的错误。您还可以使用 pp allocates(:banner) 转储整个横幅以查看其所有属性。
很抱歉没有提供更多帮助。我本来可以在评论中写下这个,但我需要良好的格式。
There doesn't seem to be any obvious problem with your code that might cause this. One thing you should note is that you have a uniqueness constraint on
name
, butvalid_attributes
always returns the same name. That's not where your problem is, I'm just mentioning it as something to keep in mind.I can only give you advice on how to try and debug this yourself. You can access the newly created banner in the spec as
assigns(:banner)
. So, you could do something like this:This will dump the banner's errors to the console where the tests are running. If the banner is not being saved, this should mean it's not valid, so checking what errors are generated is the first thing to do. You could also dump the entire banner with
pp assigns(:banner)
to see all of its attributes.Sorry for not being more helpful. I would have written this in a comment, but I needed the nice formatting.