Ruby on rails 访问模型&x27;s场
我正在使用rails迈出第一步,正在阅读m.Hartl的教程。我正在尝试编写一些简单的测试,以检查在删除用户时是否删除了以下关系Ruby on rails 访问模型&x27;s场,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在使用rails迈出第一步,正在阅读m.Hartl的教程。我正在尝试编写一些简单的测试,以检查在删除用户时是否删除了以下关系 describe "deleting user should delete relationship" do before do other_user.destroy end expect(@user.name).to eq("Tom") # undefined method name expect(@user.followers).to i
describe "deleting user should delete relationship" do
before do
other_user.destroy
end
expect(@user.name).to eq("Tom") # undefined method name
expect(@user.followers).to include(other_user) # undefined method followers
its(:followers) { should_not include(other_user) } # this works
end
为什么没有定义@user.name
和@user.followers
?我如何访问它?我希望至少有一个名字,因为它是用户模型的属性。。。它是私有的,我需要写访问器才能访问它吗?追随者也是如此。。我是否必须编写单独的方法来返回用户的追随者
用户模型如下所示:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower # we could omit source in this case since, in the case of a :followers attribute, Rails will singularize “followers” and automatically look for the foreign key follower_id in this case.
...
its(:name) { should eq 'Tom' }
its(:followers) { should_not include(other_user) }
class用户
您不能在测试之外使用期望-您需要将它们包装在:
it 'name is Tom' do
expect(@user.name).to eq 'Tom'
end
it 'is followed by deleted user' do
expect(@user.followers).to include(other_user)
end
它的
方法是一个方便的快捷方式:
its(:attribute) { should be_present } # test name: Its attribute should be present
与相同(除了更好的测试名称):
请注意,您需要在测试套件中定义主题
,才能使用第二种形式。如果已经定义了主题,那么应该使用它而不是实例变量(这样就可以轻松地更改主题,而不必更改所有测试)。因此,您的测试应该如下所示:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower # we could omit source in this case since, in the case of a :followers attribute, Rails will singularize “followers” and automatically look for the foreign key follower_id in this case.
...
its(:name) { should eq 'Tom' }
its(:followers) { should_not include(other_user) }
它没有按您编写的方式工作的原因是,
@user
是一个实例变量,您试图在类的上下文中执行它。这自然会返回“nil”,因为类没有定义这样的变量。您不能在测试之外使用期望-您需要将它们包装在:
it 'name is Tom' do
expect(@user.name).to eq 'Tom'
end
it 'is followed by deleted user' do
expect(@user.followers).to include(other_user)
end
它的
方法是一个方便的快捷方式:
its(:attribute) { should be_present } # test name: Its attribute should be present
与相同(除了更好的测试名称):
请注意,您需要在测试套件中定义主题
,才能使用第二种形式。如果已经定义了主题,那么应该使用它而不是实例变量(这样就可以轻松地更改主题,而不必更改所有测试)。因此,您的测试应该如下所示:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower # we could omit source in this case since, in the case of a :followers attribute, Rails will singularize “followers” and automatically look for the foreign key follower_id in this case.
...
its(:name) { should eq 'Tom' }
its(:followers) { should_not include(other_user) }
它没有按您编写的方式工作的原因是,
@user
是一个实例变量,您试图在类的上下文中执行它。这自然会返回“nil”,因为类没有定义这样的变量。您不能在测试之外使用期望-您需要将它们包装在:
it 'name is Tom' do
expect(@user.name).to eq 'Tom'
end
it 'is followed by deleted user' do
expect(@user.followers).to include(other_user)
end
它的
方法是一个方便的快捷方式:
its(:attribute) { should be_present } # test name: Its attribute should be present
与相同(除了更好的测试名称):
请注意,您需要在测试套件中定义主题
,才能使用第二种形式。如果已经定义了主题,那么应该使用它而不是实例变量(这样就可以轻松地更改主题,而不必更改所有测试)。因此,您的测试应该如下所示:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower # we could omit source in this case since, in the case of a :followers attribute, Rails will singularize “followers” and automatically look for the foreign key follower_id in this case.
...
its(:name) { should eq 'Tom' }
its(:followers) { should_not include(other_user) }
它没有按您编写的方式工作的原因是,
@user
是一个实例变量,您试图在类的上下文中执行它。这自然会返回“nil”,因为类没有定义这样的变量。您不能在测试之外使用期望-您需要将它们包装在:
it 'name is Tom' do
expect(@user.name).to eq 'Tom'
end
it 'is followed by deleted user' do
expect(@user.followers).to include(other_user)
end
它的
方法是一个方便的快捷方式:
its(:attribute) { should be_present } # test name: Its attribute should be present
与相同(除了更好的测试名称):
请注意,您需要在测试套件中定义主题
,才能使用第二种形式。如果已经定义了主题,那么应该使用它而不是实例变量(这样就可以轻松地更改主题,而不必更改所有测试)。因此,您的测试应该如下所示:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower # we could omit source in this case since, in the case of a :followers attribute, Rails will singularize “followers” and automatically look for the foreign key follower_id in this case.
...
its(:name) { should eq 'Tom' }
its(:followers) { should_not include(other_user) }
它没有按您编写的方式工作的原因是,
@user
是一个实例变量,您试图在类的上下文中执行它。这自然会返回“nil”,因为类没有定义这样的变量。您必须模仿您的object@user。简单的方法是
let(@user){ User.new(name: "Foo") }
此实例将新用户关联到@User。
我更喜欢用FactoryGirl来做这件事。但是User.new将解决您的问题您必须模仿您的object@User。简单的方法是
let(@user){ User.new(name: "Foo") }
此实例将新用户关联到@User。
我更喜欢用FactoryGirl来做这件事。但是User.new将解决您的问题您必须模仿您的object@User。简单的方法是
let(@user){ User.new(name: "Foo") }
此实例将新用户关联到@User。
我更喜欢用FactoryGirl来做这件事。但是User.new将解决您的问题您必须模仿您的object@User。简单的方法是
let(@user){ User.new(name: "Foo") }
此实例将新用户关联到@User。
我更喜欢用FactoryGirl来做这件事。但是User.new将解决您的问题