Tag: rspec

Rspec视图未定义方法stub_model

我试图找出这段代码有什么问题。 即它找不到方法’stub_model’。 试图寻找解决方案,但在任何地方我看起来我的文件似乎是好的。 请看看它也许我只是看不到一个简单的错误。 非常感谢:)书籍模型在db中创建。 我的观点规范(spec / view / books_spec.rb)如下所示: require ‘rails_helper’ describe ‘books/new’ do it ‘displays the book form’ do book = stub_model(Book) assign(:book, book) render expect(rendered).to have_selector(“form label[for *= ‘Title’]”) expect(rendered).to have_selector(“form label[for *= ‘Author’]”) expect(rendered).to have_selector(“form label[for *= ‘Cover Photo’]”) expect(rendered).to have_button “Add Book” end end 并且错误如下: 1) books/new displays the book […]

使用Rspec连接链式方法

我想调用一个只返回一条记录的named_scope,但是named_scope返回一个数组,这不是什么大问题,因为我可以用.first链接它。 Model.named_scope(param).first 这是有效的,我正在努力的是如何存根链接的电话。 有没有人有关于如何通过Rspec嘲笑来实现这一目标的参考或答案?

如何在RSpec中设置request.referrer?

我试图避免使用我的session[:referred_by] ,并希望使用request.referrer 。 但是,我的RSpec测试失败,因为TestRequest不存储request.referrer 所以我必须执行以下操作才能使Rspec测试工作。 有没有办法让它变得更好: referrer = request.referrer ? request.referrer : ‘/’ redirect_to referrer, :alert => error_message

测试“创建”控制器操作的正确方法是什么?

我使用Ruby on Rails 3.2.2,Rspec 2.9.0和RspecRails 2.9.0。 我想测试create控制器动作,但我不知道如何使“正确”/“正确”的方式。 我“搭建”模型,控制器,视图,…文件,所以在那些文件中我有Ruby on Rails生成器生成的公共代码; 在我的spec文件中我有: it “assigns @article” do new_article = FactoryGirl.build(:article) Article.should_receive(:new).and_return(new_article) post :create assigns[:article].should eq(new_article) end 也许,( 注意 :上面的代码几乎与我用来测试new控制器动作的代码相同) 测试create控制器动作的更好方法是在post :create期间传递一些属性值post :create action而不是像我做的那样继续以上 ,但我不知道如何制作它,如果它是“正确”/“适当”的方式来制作东西。 那么, 测试“创建”控制器动作的正确方法是什么?

在RSpec中需要lib,Ruby 1.9.2带来“没有这样的文件加载”

我正在尝试将我的一个Rails项目升级到Ruby 1.9.2 。 一切都很顺利,但一次RSpec测试破裂了。 在这个测试中我require一个Ruby lib : # file spec/models/my_lib_spec.rb require ‘spec_helper’ require ‘lib/services/my_lib’ describe “MyLib” do it “should do something” do … lib看起来像这样: # file lib/services/my_lib.rb class MyLib def self.do_something … 在Ruby 1.8.7(REE)中,测试运行良好: $ ruby -v ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin11.1.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03 $ rspec ./spec/models/my_lib_spec.rb .. Finished in […]

rspec路由测试和主机

我看到我可以像这样用rspec测试路由: get(“/”).should route_to(“welcome#index”) 但是我有基于主机名或主机名部分的约束以及几个主机名之间的重定向。 如何在测试时指定主机名? 如何使用正确的配置运行测试? 我试过打印root_url,我得到了: 缺少主机链接! 请提供:host参数,设置default_url_options [:host],或将:only_path设置为true

在现有路线的rspec测试中找不到路线

耙路线显示: estimate_location GET /estimate/location/:id(.:format) estimate/location#show 我的rspec测试: it ‘should re-direct to location/new from show’ do e = FactoryGirl.create :estimate get estimate_location_path e expect(response.status).to eq(302) end 控制台说: Failure/Error: get estimate_location_path e ActionController::RoutingError: No route matches {:controller=>”estimate/location”, :action=>”/estimate/location/1″} 这对我来说没有意义。 有一条路线,我传递了一个对象(Rails巧妙地抓住了ID),但是它说没有这样的路径?

RSpec:如何编写unit testing用例来接收在私有方法中引发的exception

我已经为Race Condition实施了乐观锁定。 为此,我在Product中添加了一个额外的列lock_version 。 方法: recalculate是调用private method_1然后保存( save! )产品。 我不能用save! 在私有method_1 ,因为它将失败其他东西。 我不想重构业务逻辑。 #Product: Model’s new field: # lock_version :integer(4) default(0), not null def recalculate method_1 self.save! end private def method_1 begin #### #### if self.lock_version == Product.find(self.id).lock_version Product.where(:id => self.id).update_all(attributes) else raise ActiveRecord::StaleObjectError.new(self, “test”) end rescue ActiveRecord::StaleObjectError => e if tries < 3 tries […]

在Ruby On Rails上学习BDD的途径是什么?

我想在Ruby On Rails上启动BDD我应该学习什么? 我对BDD,RSpec或Cucumber一无所知。 什么是最好的学习方法? 教程? 包含“我应该测试什么样的行为?”之类的东西。 等等 谢谢!

在何处/如何包含用于水豚集成测试的辅助方法

我正在使用capybara进行集成/验收测试。它们位于/spec/requests/文件夹中。现在我在验收测试中使用了一些辅助方法。一个例子是register_user ,看起来像这样 def register_user(user) visit home_page fill_in ‘user_name’, :with => user.username fill_in ‘password’, :with => user.password click_button ‘sign_up_button’ end 我想在几种不同的验收测试中使用这种方法(它们在不同的文件中)。 包含此内容的最佳方式是什么? 我已经尝试将它放在spec/support/但它并没有为我工作。 花了一些时间后,我意识到我甚至不知道这是否是一个好方法,所以我想我会在这里问。 注意:我使用的是rails 3,spork和rspec。