Tag: 应该是

工厂女孩有很多关系(和受保护的属性)

我有这种关系: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end 控制器内部的默认方案如下: @article = Article.create(:title => “foobar”) @comment = @article.comments.create(:content => “w00t”) 我试过写这些工厂: Factory.define :article do |f| f.title “Hello, world” end Factory.define :comment do |f| f.content “Awesome!” f.association :article end 但我的语法关联不正确。 由于评论的article_id protected属性,这有点棘手。 所以我认为如果我在文章工厂内声明关联应该会更好,但我不知道如何处理。 谢谢你的帮助。

如何使用shoulda匹配器来测试多态分析?

我正在使用带有rails的shoulda-matcher,我正在创建一个名为“comments”的模型和另一个名为“post”的模型。 评论是多态的。 当我在post中使用shoulda匹配器进行测试时,就像这样 it {should have_many(:comments)} 得到这个消息 期望Post有一个名为comments的has_many关联(Comment没有post_id外键。) 在我的评论模型中,我有 belongs_to :commentable, :polymorphic => true 如何测试我的多态关联,以便post可以有很多注释? ps the shoulda matcher文档说它支持多态关联。

工厂女孩什么时候在db中创建对象?

我正在尝试使用FactoryGirl / shoulda模拟会话(它与灯具一起工作但我在使用工厂时遇到问题)。 我有以下工厂(用户登录和电子邮件都有uniquevalidation): Factory.define :user do |u| u.login ‘quentin’ u.email ‘quentin@example.com’ end Factory.define :session_user, :class => Session do |ses| ses.association :user, :factory => :user ses.session_id ‘session_user’ end 这是测试 class MessagesControllerTest < ActionController::TestCase context "normal user" do setup do @request.session[:user_id]=Factory(:user).id @request.session[:session_id]=Factory(:session_user).session_id end should "be able to access new message creation" do get :new assert_response :success […]