Rails:功能测试中的工厂和协会
我试图找出为什么这不起作用。
假设您有三个模型:User、Foo 和 Bar。为了创建 bar,用户必须首先创建并验证 foo 对象。
Class User #snip!
has_many :foos
has_many :bars
Class Foo #snip!
belongs_to :user
has_many :bars
Class Bar #snip!
belongs_to :user
belongs_to :foo
我正在尝试进行功能测试,如果用户尝试在没有有效 foo 的情况下创建一个新 Bar,他们会被重定向到 Foo 的“新”操作。
我对重定向场景没有任何问题。但是,当我尝试使用有效的 Foo 对象设置用户并尝试获取 Bar 的“新”操作时,它仍然会被重定向到 Foo 控制器的“新”操作。它仍然不承认用户有 Foo。
这是我的控制器:
class BarsControllerTest < ActionController::TestCase
setup :activate_authlogic
def setup
@request.env['HTTPS'] = nil
@user = Factory.build(:user)
@foo = Factory.build(:foo, :user => @user)
end
test "should get new when user has a valid foo" do
@request.env['HTTPS'] = 'on'
UserSession.create(@user)
get :new
assert_response :success
end
这是我的应用程序控制器中的重定向函数,在我的 bar 控制器中调用:
def foo_required
if current_user && @current_user.foos.valid.empty? && @current_user.foos.empty?
flash[:notice] = "You must have a verified foo in order to create a Bar!"
redirect_to new_foo_path
elsif current_user && @current_user.foos.valid.empty?
flash[:notice] = "You must verify your foos in order to create a Bar!"
redirect_to foos_path
end
end
这是 Foo Factory:
Factory.define :foo do |f|
#attributes
f.valid true
f.association :user
end
相反,我被重定向到“https://test.host:80/foos/new" 控制器不承认用户有 Foo...
会话有效,所以这看起来就像工厂的问题,但我不确定它是什么。
I'm trying to figure out why this doesn't work.
Let's say you three models, User, Foo and Bar. In order for a bar to be created the user must first create and validate a foo object.
Class User #snip!
has_many :foos
has_many :bars
Class Foo #snip!
belongs_to :user
has_many :bars
Class Bar #snip!
belongs_to :user
belongs_to :foo
I'm trying to get a functional test working where if the user tries to make a new Bar without having a valid foo, they get redirected to the "new" action for a Foo.
I haven't had any problem with the redirect scenario. However, when I try to setup a user with a valid Foo object and try to get the "new" action for a Bar, it still gets redirected to the "new" action of the Foo controller. It still doesn't acknowledge that the User has a Foo.
Here's my controller:
class BarsControllerTest < ActionController::TestCase
setup :activate_authlogic
def setup
@request.env['HTTPS'] = nil
@user = Factory.build(:user)
@foo = Factory.build(:foo, :user => @user)
end
test "should get new when user has a valid foo" do
@request.env['HTTPS'] = 'on'
UserSession.create(@user)
get :new
assert_response :success
end
This is the redirect function I have in my application controller which is called in my bar controller:
def foo_required
if current_user && @current_user.foos.valid.empty? && @current_user.foos.empty?
flash[:notice] = "You must have a verified foo in order to create a Bar!"
redirect_to new_foo_path
elsif current_user && @current_user.foos.valid.empty?
flash[:notice] = "You must verify your foos in order to create a Bar!"
redirect_to foos_path
end
end
Here's the Foo Factory:
Factory.define :foo do |f|
#attributes
f.valid true
f.association :user
end
Instead I get redirected to "https://test.host:80/foos/new" The controller doesn't acknowledge that the user has a Foo...
The session is valid so this seems like a factory problem, but I'm not sure what it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设这是factory_girl。您正在调用
Factory.build
,它不会将任何内容保存到数据库中,因此您永远不会拥有关联所需的外键值。将它们切换到 Factory.create ,您应该会看到差异。I'm assuming that this is factory_girl. You are calling
Factory.build
which doesn't save anything to the database, so you never have the foreign key value needed for your association. Switch those toFactory.create
and you should see a difference.