Tag: rspec

辅助测试中未定义的局部变量或方法“main_app”

我在Rails主应用程序上运行我的帮助程序测试时遇到错误(带有更多插件) 1) MenuHelper maintence menu Failure/Error: before { menu = build_menu_maintence() } NameError: undefined local variable or method `main_app’ for # # ./app/helpers/menu_helper.rb:37:in `eval’ # (eval):1:in `block in build_menu_items’ # ./app/helpers/menu_helper.rb:37:in `eval’ # ./app/helpers/menu_helper.rb:37:in `block in build_menu_items’ # ./app/helpers/menu_helper.rb:23:in `each’ # ./app/helpers/menu_helper.rb:23:in `build_menu_items’ # ./app/helpers/menu_helper.rb:15:in `build_menu’ # ./app/helpers/menu_helper.rb:48:in `build_menu_maintence’ # ./spec/helpers/menu_helper_spec.rb:11:in `block (3 levels) in […]

测试has_many与RSpec的关联

我正在尝试使用RSpec测试Hour模型,即类似于范围的类方法’find_days_with_no_hours’。 业务有多个与STI相关的小时数。 需要通过Business对象调用find_days_with_no_hours,我无法弄清楚如何在RSpec测试中设置它。 我希望能够测试类似的东西: bh = @business.hours.find_days_with_no_hours bh.length.should == 2 我尝试了各种方法,比如创建一个Business对象(比如说Business.create),然后设置@ business.hours << mock_model(BusinessHour,…,…,…)但是没有工作。 这通常是怎么做的? class Business :hourable end class Hour true def self.find_days_with_no_hours where(“start_time IS NULL”) end end

在MacVim中使用rails.vim Rake运行规范,如何获得彩色输出

当使用Rake运行规范时,quickfix中的输出不会着色,这意味着它没有绿色表示传递规格,红色表示规格失败。 这是可以解决的吗? 谢谢你的任何提示。

无效选项: – autospec

/usr/local/bin/ruby -rrubygems -e “require ‘redgreen'” /usr/local/lib/ruby/gems/1.8/gems/rspec-1.3.0/bin/spec –autospec -O spec/spec.opts invalid option: –autospec Test::Unit automatic runner. Usage: -e [options] [– untouched arguments] which spec says “/usr/local/bin/spec” which autospec says “/usr/local/bin/autospec” /usr/local/bin/spec –help includes –autospec option in the list. 我在这里想念的是什么?

在rails初始化程序运行之前,在“之前”块中运行rspec

我想before块before运行一个rspec来在Rails初始化器运行之前设置一些东西,所以我可以测试初始化​​器应该做什么。 这可能吗?

使用rspec和capybara测试rails + backbone应用程序

我正在使用列出的gem为我的应用程序编写测试。 我找不到如何设置Capybara与骨干(ajax in all)一起工作 示例测试: require ‘spec_helper’ describe “Main” do describe “GET /” do it “displays articles” do Article.create!(title:’title’,body:’body text’) visit ‘/’ page.should have_content(‘body text’) end end end 和测试的输出: Failures: 1) Main GET / displays articles Failure/Error: page.should have_content(‘body text’) expected there to be text “body text” in “Loading…” # ./spec/features/main_spec.rb:8:in `block (3 levels) in […]

shoulda匹配器应该validate_uniqueness_of与范围失败

我有3个型号,用户,游戏和播放器。 用户和游戏之间基本上存在多对多的关系,玩家作为联接表,除了玩家有其他信息,因此它有自己的模型。 玩家需要游戏ID和用户ID的唯一组合,所以我试着在玩家中说: validates_uniqueness_of :user_id, :scope => :game_id 然后在我的规范中,我说(使用shoulda matchers): it { should validate_uniqueness_of(:user_id).scoped_to(:game_id)} 以下是玩家定义的关系: belongs_to :game, :inverse_of => :players belongs_to :user, :inverse_of => :players 但我在该规范上得到一个ActiveRecord :: statementinvalid错误 ActiveRecord::StatementInvalid: Mysql2::Error: Column ‘game_id’ cannot be null: INSERT INTO `players` ETC… 知道出了什么问题吗?

Rspec:在路由规范中添加一些头请求

我正在开发一个Rails应用程序,它具有JSON格式的REST API并且版本化(根据这个优秀的Ryan演员: http : //railscasts.com/episodes/350-rest-api-versioning )。 例如,有一个spec / requests规范: require ‘spec_helper’ describe “My Friends” do describe “GET /my/friends.json” do it “should get my_friends_path” do get v1_my_friends_path, {}, {‘HTTP_ACCEPT’ => ‘application/vnd.myapp+json; level=1’} response.status.should be(401) end end end 它运作良好。 但是(保持这个例子)我们如何编写路由规范? 例如,这个规范是不正确的: require ‘spec_helper’ describe “friends routing” do it “routes to #index” do get(“/my/friends.json”, nil, {‘HTTP_ACCEPT’ => ‘application/vnd.myapp+json; […]

Hartl第11章Ajax未定义局部变量或方法`cookies’

我正在进行Hartl教程,目前正在使用AJAX进行第11.2.5章工作跟踪按钮。 对于关系控制器,我的测试失败了,我无法弄清楚原因。 预期的行为发生在浏览器中。 如果我删除cookie方法,它会导致许多其他测试失败。 我错过了什么导致了这个问题? 这是我对RelationshipController的测试 require ‘spec_helper’ describe RelationshipsController do let(:user) { FactoryGirl.create(:user) } let(:other_user) { FactoryGirl.create(:user) } before { sign_in user, no_capybara: true } describe “creating a relationship with Ajax” do it “should increment the Relationship count” do expect do xhr :post, :create, relationship: { followed_id: other_user.id } end.to change(Relationship, :count).by(1) end it […]

Rails / rspec:如何在请求规范中设置cookie?

如何在我的请求规范中设置cookie? 以下页面中的解决方案无效: Rspec:在帮助测试中设置cookie 也就是说, request.cookies[:whatever] = ‘something’表示该request是一个nil对象。 我也尝试了helper.cookies[:whatever] = ‘something’ ,这也没有用(在这种情况下helper是nil )。