Rails 初学者 - 为什么这个控制器测试用例失败?
好的,所以我正在开发我的第一个单独的 Rails 应用程序,一个 URL 缩短器,我已经把自己搞糊涂了。在我的模型中,我使用 domain
和 key
属性存储一个短 URL。这是我的模型:
# == Schema Information
# Schema version: 20110601022424
#
# Table name: shorteners
#
# id :integer not null, primary key
# url :string(255)
# key :string(255)
# created_at :datetime
# updated_at :datetime
# domain :string(255)
#
class Shortener < ActiveRecord::Base
attr_accessible :url
validates :url, :presence => true, :uniqueness => true, :uri_format => true
before_save :set_autogenerated_info
def shortlink
"http://#{domain}/#{key}"
end
private
def set_autogenerated_info
return unless new_record? #This only gets set one time
domain = get_random_domain
key = get_next_key(domain)
write_attribute(:domain, domain)
write_attribute(:key, key)
end
def get_random_domain
#commented out magic to grab random domain from pool
end
def get_next_key(domain)
#commented out magic to generate next unique key
end
end
我当前的方法似乎弄乱了我的controller_spec中的这个测试用例:
require 'spec_helper'
describe ShortenersController do
render_views
describe "GET show" do
before(:each) do
@shortener = Factory(:shortener)
end
it "should find the right shortener" do
get :show, :id => @shortener
assigns(:shortener).should == @shortener
end
end
end
它给了我这个错误消息:
Failures:
1) ShortenersController GET show should find the right shortener
Failure/Error: get :show, :id => @shortener
ActionView::Template::Error:
undefined local variable or method `domain' for #<Shortener:0x00000004a04548>
# ./app/models/shortener.rb:25:in `shortlink'
# ./app/views/shorteners/show.html.erb:10:in `_app_views_shorteners_show_html_erb___556550686459204284_38799300_3170414004415921977'
# ./spec/controllers/shorteners_controller_spec.rb:32:in `block (3 levels) in <top (required)>'
我可以通过在 attr_accessible
行上方添加它来让测试用例通过:
attr_reader :domain, :key
但这确实做了一些非常疯狂的事情,例如不在我的应用程序视图中显示域/键属性,甚至不允许我直接从 Rails 控制台中的模型访问它们:
Loading development environment (Rails 3.0.7)
>> s = Shortener.new(:url => 'http://www.stackoverflow.com')
=> #<Shortener id: nil, url: "http://www.stackoverflow.com", key: nil, created_at: nil, updated_at: nil, domain: nil>
>> s.save
=> true
>> s
=> #<Shortener id: 12, url: "http://www.stackoverflow.com", key: l, created_at: "2011-06-02 16:35:01", updated_at: "2011-06-02 16:35:01", domain: "localhost">
>> s.domain
=> nil
>> s.key
=> nil
>> s.shortlink
=> "http:///"
更新 - 添加视图:
<p id="notice"><%= notice %></p>
<p>
<b>Url:</b>
<%= @shortener.url %>
</p>
<p>
<b>Shortened Link:</b>
<a href="<%= @shortener.shortlink %>"><%= @shortener.shortlink %></a>
</p>
<%= link_to 'Edit', edit_shortener_path(@shortener) %> |
<%= link_to 'Back', shorteners_path %>
OK, so I'm working on my first solo Rails app, a URL shortener, and I've already confused myself pretty well. From my model, I store a short URL using the domain
and key
attributes. Here is my model:
# == Schema Information
# Schema version: 20110601022424
#
# Table name: shorteners
#
# id :integer not null, primary key
# url :string(255)
# key :string(255)
# created_at :datetime
# updated_at :datetime
# domain :string(255)
#
class Shortener < ActiveRecord::Base
attr_accessible :url
validates :url, :presence => true, :uniqueness => true, :uri_format => true
before_save :set_autogenerated_info
def shortlink
"http://#{domain}/#{key}"
end
private
def set_autogenerated_info
return unless new_record? #This only gets set one time
domain = get_random_domain
key = get_next_key(domain)
write_attribute(:domain, domain)
write_attribute(:key, key)
end
def get_random_domain
#commented out magic to grab random domain from pool
end
def get_next_key(domain)
#commented out magic to generate next unique key
end
end
My current approach seems to be messing up this test case in my controller_spec:
require 'spec_helper'
describe ShortenersController do
render_views
describe "GET show" do
before(:each) do
@shortener = Factory(:shortener)
end
it "should find the right shortener" do
get :show, :id => @shortener
assigns(:shortener).should == @shortener
end
end
end
It is giving me this error message:
Failures:
1) ShortenersController GET show should find the right shortener
Failure/Error: get :show, :id => @shortener
ActionView::Template::Error:
undefined local variable or method `domain' for #<Shortener:0x00000004a04548>
# ./app/models/shortener.rb:25:in `shortlink'
# ./app/views/shorteners/show.html.erb:10:in `_app_views_shorteners_show_html_erb___556550686459204284_38799300_3170414004415921977'
# ./spec/controllers/shorteners_controller_spec.rb:32:in `block (3 levels) in <top (required)>'
I can get the test case to pass by adding this above the attr_accessible
line:
attr_reader :domain, :key
But that does some really crazy things like not displaying domain/key attributes in my application's views and not letting me even access them directly from the model in my Rails console:
Loading development environment (Rails 3.0.7)
>> s = Shortener.new(:url => 'http://www.stackoverflow.com')
=> #<Shortener id: nil, url: "http://www.stackoverflow.com", key: nil, created_at: nil, updated_at: nil, domain: nil>
>> s.save
=> true
>> s
=> #<Shortener id: 12, url: "http://www.stackoverflow.com", key: l, created_at: "2011-06-02 16:35:01", updated_at: "2011-06-02 16:35:01", domain: "localhost">
>> s.domain
=> nil
>> s.key
=> nil
>> s.shortlink
=> "http:///"
Update - added view:
<p id="notice"><%= notice %></p>
<p>
<b>Url:</b>
<%= @shortener.url %>
</p>
<p>
<b>Shortened Link:</b>
<a href="<%= @shortener.shortlink %>"><%= @shortener.shortlink %></a>
</p>
<%= link_to 'Edit', edit_shortener_path(@shortener) %> |
<%= link_to 'Back', shorteners_path %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在测试中,Shortener 类似乎无法访问域变量。你检查过你的测试数据库吗?
It seems that in the tests, the Shortener class has no access to the domain variable. Have you checked your Test DB?