轨道/ Rspec的 - 写规范的belongs_to的关联类的名称 [英] Rails / Rspec - writing spec for class name of belongs_to association

查看:156
本文介绍了轨道/ Rspec的 - 写规范的belongs_to的关联类的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于低于code:

(1)你怎么会写一个规范来测试HOME_TEAM和AWAY_TEAM的类名应该是一个Team类?

(2)如果你不屑于写这样的规范吗?我不知道我看到这样做的价值,但希望得到你的看法。

 类的事件< ActiveRecord的::基地  belongs_to的:HOME_TEAM,:CLASS_NAME => 团队,:foreign_key => :home_team_id
  belongs_to的:AWAY_TEAM,:CLASS_NAME => 团队,:foreign_key => :away_team_id结束描述事件做  它{应当belong_to(:HOME_TEAM)}
  它{应当belong_to(:AWAY_TEAM)}结束

将是很好,如果早该有这样的事情:

  {它应该belong_to(:HOME_TEAM).with_class_name(:队)}


解决方案

下面是关于为什么这真的是不应该做的博客文章:

<一个href=\"http://blog.davidchelimsky.net/2012/02/12/validations-are-behavior-associations-are-structure/\" rel=\"nofollow\">http://blog.davidchelimsky.net/2012/02/12/validations-are-behavior-associations-are-structure/

要总结,协会是你的应用程序的结构。 RSpec的是为了测试行为。因此,它可能会更好,如果你写为从HOME_TEAM或AWAY_TEAM衍生的行为测试。

举个例子,如果你想要的HOME_TEAM的名称。这将是更好,如果你写了这样的方法:

 高清HOME_TEAM_NAME
  home_team.name
结束

这是该事件类将索要HOME_TEAM行为。你可以写一个规范,如:

 描述'#home_team_name做
  之前做
    @home_team = Team.new(:名称=&GT;'主队')
    @event = Event.new(home_team_id:@ home_team.id)
  结束  它应该返回主队的名字'做
    @ event.home_team_name.should =='主队'
  结束
结束

这将是测试协会的行为,而不是直接测试您的应用程序的结构的一个很好的例子。

此外,作为一个点,滑轨期望HOME_TEAM是类团队,并且也没有必要进行测试的框架内,它是很好的测试和记录。我能看到这样做将是一个暂时的基础上,如果你需要提高你的那assocaitions工作方式的理解的唯一途径。

Given the code below:

(1) How would you write a spec to test that the class name of home_team and away_team should be a Team class?

(2) Should you even bother to write such a spec? I'm not sure I see the value in doing so, but wanted to get your thoughts.

class Event < ActiveRecord::Base

  belongs_to :home_team, :class_name => 'Team', :foreign_key => :home_team_id
  belongs_to :away_team, :class_name => 'Team', :foreign_key => :away_team_id

end

describe Event do

  it { should belong_to(:home_team) }
  it { should belong_to(:away_team) }

end

Would be nice if shoulda had something like:

it { should belong_to(:home_team).with_class_name(:team) }

解决方案

Here is a blog post about why this really shouldn't be done:

http://blog.davidchelimsky.net/2012/02/12/validations-are-behavior-associations-are-structure/

To summarize, associations are the structure of your application. RSpec is meant to test behaviors. So, its probably better if you write tests for the behavior that is derived from the home_team or the away_team.

Take for example if you wanted the name of the home_team. It would be better if you wrote a method like this:

def home_team_name
  home_team.name
end

This is a behavior that the event class would ask the home_team for. You could write a spec such as:

describe '#home_team_name' do
  before do
    @home_team = Team.new(:name => 'The Home Team')
    @event = Event.new(home_team_id: @home_team.id)
  end

  it 'should return the name of the home team' do
    @event.home_team_name.should == 'The Home Team'
  end
end 

This would be a great example of testing the behavior of the association, without directly testing the structure of your application.

Also, as a point, Rails expects that home_team is of class 'Team', and there is no need to test the framework, it is well tested and documented. The only way that I could see doing this would be on a temporary basis, if you needed to improve your understanding of the way that assocaitions work.

这篇关于轨道/ Rspec的 - 写规范的belongs_to的关联类的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆