shoulda

Shoulda belongs_to with class_name and foreign_key

限于喜欢 提交于 2020-03-21 10:57:33
问题 I know you can easily test a belongs to relationship using Shoulda: describe Dog dog it { should belong_to(:owner) } end Is it possible to test a more complicated belongs_to relationship using Shoulda? Something like this: class Dog < ActiveRecord::Base belongs_to :owner, :class_name => "Person", :foreign_key => "person_id" end 回答1: You should be able to use: it { should belong_to(:owner).class_name('Person') } Shoulda's belong_to matcher always reads the foreign_key from the association and

RSpec - Undefined method `key?'

戏子无情 提交于 2020-02-23 07:18:18
问题 I'm trying to run a test for rendering templates and ran into the error: undefined method `key?' for 1014:Fixnum The stub of my model's instance works as it should in my route tests but not so much here. What am I doing wrong? describe RestaurantsController do let(:restaurant) { FactoryGirl.build_stubbed(:restaurant) } describe 'GET #show' do before { get :show, restaurant.id } it { should render_template('show') } end end Full Error 1) RestaurantsController GET #show Failure/Error: before {

Why should I use RSpec or shoulda with Rails?

﹥>﹥吖頭↗ 提交于 2020-01-30 14:13:27
问题 I am setting up a rails app and I just finished making some unit tests and my friend said that apparently fixtures are no longer cool and people are now using RSpec or shoulda. I was wondering what the actual benefits are to use these other toolkits. Any information at all is appreciated. -fREW 回答1: RSpec and similar frameworks are tooling designed to aid in Behavior Driven Development. They're not just a prettier way to write tests, though they do help with that. There is plenty of

Why should I use RSpec or shoulda with Rails?

我们两清 提交于 2020-01-30 14:11:11
问题 I am setting up a rails app and I just finished making some unit tests and my friend said that apparently fixtures are no longer cool and people are now using RSpec or shoulda. I was wondering what the actual benefits are to use these other toolkits. Any information at all is appreciated. -fREW 回答1: RSpec and similar frameworks are tooling designed to aid in Behavior Driven Development. They're not just a prettier way to write tests, though they do help with that. There is plenty of

Factory Girl with has many relationship (and a protected attribute)

我是研究僧i 提交于 2020-01-04 04:02:44
问题 I have this kind of relation: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end The default scenario inside controllers looks like: @article = Article.create(:title => "foobar") @comment = @article.comments.create(:content => "w00t") I had tried to write those factories: Factory.define :article do |f| f.title "Hello, world" end Factory.define :comment do |f| f.content "Awesome!" f.association

Factory Girl with has many relationship (and a protected attribute)

我的梦境 提交于 2020-01-04 04:02:44
问题 I have this kind of relation: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end The default scenario inside controllers looks like: @article = Article.create(:title => "foobar") @comment = @article.comments.create(:content => "w00t") I had tried to write those factories: Factory.define :article do |f| f.title "Hello, world" end Factory.define :comment do |f| f.content "Awesome!" f.association

RSpec vs. Shoulda?

烈酒焚心 提交于 2020-01-03 07:26:09
问题 I am new to the unit testing scene; I have only been using unit tests for about 2 months now. When I unit test in Ruby I currently follow the TDD style and use Test::Unit::TestCase. I have also read about RSpec and how it follows the BDD methodology. I also have read about Shoulda which is a in between the two frameworks. My question is, which framework should I devote my time to RSpec or Shoulda, or should I stick with Test::Unit::TestCase? Any opinions on the subject are appreciated. 回答1:

Rails Can't find route in Functional Test

こ雲淡風輕ζ 提交于 2019-12-24 08:51:30
问题 Rails version 3.0.4 and Ruby 1.9.2 I'm using the devise gem, and have my application set up so that a user must sign in to perform any action. While writing my functional tests, I found that all of the test threw the same error. `method_missing': undefined method `new_user_session_path' This is strange as, if I rake routes I can see new_user_session GET /devise/login(.:format) {:action=>"new", :controller=>"devise/sessions"} I can also confirm that when I run the application, all of the link

Unable to test should belong_to, missing id foreign key on Rails

戏子无情 提交于 2019-12-22 12:04:29
问题 Hello I have been looking for a way to test model relationship and stumble upon should gem shoulda (3.5.0) shoulda-context (1.2.1) shoulda-matchers (2.8.0) Unfortunatly I tried to test a simple example with rspec describe Region do it "should have a city" do should belong_to(:city) end end and I always get a message of Region should have a city Failure/Error: should belong_to(:city) Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.) # .

Unable to test should belong_to, missing id foreign key on Rails

爱⌒轻易说出口 提交于 2019-12-22 12:04:27
问题 Hello I have been looking for a way to test model relationship and stumble upon should gem shoulda (3.5.0) shoulda-context (1.2.1) shoulda-matchers (2.8.0) Unfortunatly I tried to test a simple example with rspec describe Region do it "should have a city" do should belong_to(:city) end end and I always get a message of Region should have a city Failure/Error: should belong_to(:city) Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.) # .