Tag: rspec

用于创建ajax关系的关系控制器-NoMethodError:未定义的方法` _url’

我相信我的控制器中的redirect_to出现问题。 课程有很多级别,有很多步骤(步骤属于级别,级别属于课程)。 我正在尝试重定向到该步骤。 这是我迈出一步的路线: /courses/:course_id/levels/:level_id/steps/:id(.:format) 这是我在控制器中的错误: Failure/Error: click_button “check answer” NoMethodError: undefined method `step_url’ for # 这是发生错误的控制器: class UserStepsController < ApplicationController before_filter :authenticate_user! def create @step = Step.find(params[:user_step][:step_id]) current_user.attempt_step!(@step) respond_to do |format| format.html { redirect_to @step } format.js end end def destroy @step = UserStep.find(params[:id]).step current_user.remove_user_step!(@step) respond_to do |format| format.html { redirect_to @step } format.js […]

Shoulda Matcher和has_many通过:未定义的方法`class_name’为nil:NilClass

我试图通过与Rspec和Shoulda匹配器的关系测试a:has_many。 # student.rb has_many :presences has_many :calls, through: :presences # student_spec.rb it { should have_many(:presences) } it { should have_many(:calls).through(:presences) } #presence.rb belongs_to :call belongs_to :student #presence_spec.rb it { should belong_to(:call) } it { should belong_to(:student) } #call.rb has_many :presences has_many :students, through: :presences #call_spec.rb it { should have_many(:presences) } it { should have_many(:students).through(:presences) } […]

为什么我的Rspec发布:创建无效的参数控制器测试失败?

我的控制器有两个测试。 spec/controllers/employees_controller_spec.rb it ‘should create a new valid employee’ do employee_params = FactoryGirl.attributes_for(:employee) expect { post :create, employee: employee_params }.to change(Employee, :count).by(1) should redirect_to(action: :index) end it ‘should not save an employee with an invalid avatar extention’ do invalid_employee_params = FactoryGirl.attributes_for(:employee) invalid_employee_params[:avatar] = Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, ‘/spec/fixtures/photos/duck.txt’))) expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count).by(1) end […]

如何以及分别使用Cucumber和Rspec进行测试?

我正在使用Ruby on Rails 3.2.2,cucumber-rails-1.3.0,rspec-rails-2.8.1和capybara-1.1.2以及Selenium驱动程序。 我想分别使用Cucumber和Rspec来了解如何测试和测试什么 ? 也就是说,我应该用Cucumber处理什么“行为”以及Rspec如何以适当的方式划分测试“关注点”? 例如 ,为了测试视图,我应该使用Cucumber还是RSpec测试页面内容中是否存在CSS id ? 或两者?

关于Ruby Tutorial第9章的RSPEC错误

我在mac osx10.8上使用ruby 1.8.7。 我不知道这些错误是什么意思。 我目前正处于ruby on rails教程的第9章末尾。 任何帮助是极大的赞赏! 1) Authentication signin with invalid information Failure/Error: before { click_button “Sign in” } NoMethodError: undefined method `[]’ for nil:NilClass # ./app/controllers/sessions_controller.rb:8:in `create’ # (eval):2:in `send’ # (eval):2:in `click_button’ # ./spec/requests/authentication_pages_spec.rb:21 2) Authentication signin with invalid information Failure/Error: before { click_button “Sign in” } NoMethodError: undefined method `[]’ […]

如何用水豚填写日期时间 – 本地字段?

我正在使用Cocoon添加记录。 ID类似于workshop_instance_sessions_attributes_1477654140_start_time 。 目前我正在遍历DOM并获取动态生成的ID(这很好),所以我可以使用它们来引用输入来填充。我已经尝试了很多东西(许多在控制台中工作)但似乎仍然无法获得工作的规范。 下面是我尝试过的一些选择( first是保存id的变量, @time基本上是Time.now,当块执行时递增)。 在控制台中工作: page.evaluate_script(“document.getElementById(‘#{first}’).value = ‘#{@time.strftime(‘%Y-%m-%eT%H:%M’)}'”) page.evaluate_script(“$(‘##{first}’).val(‘#{@time.strftime(“%Y-%m-%eT%H:%M”)}’)”) 在控制台中不起作用: fill_in first, with: @time.strftime(“%Y-%m-%eT%H:%M”) find(“##{first}”).set(@time.strftime(“%Y-%m-%eT%H:%M”)) 这些都已经在一个内部块(我遇到的一个常见问题)中,我知道正确工作正确,因为正确设置了第一个变量。 作为参考我的HTML看起来像这样: 有关如何使此规范工作的任何指示将非常感激。

使用RSpec测试奇异资源控制器

我在routes.rb定义了一个奇异的资源,看起来像这样 Rails.application.routes.draw do resource :dog, only: [:create], to: “dog#create”, controller: “dog” end 之后我用这样的创建动作定义了一个控制器 class DogController < ApplicationController def create render json: {}, status: :ok end end 而现在我正试图用这样的RSpec来测试它 require “rails_helper” describe DogController do it “works” do post :create, params: { foo: :bar } end end 这是抛出此错误而不是传递: ActionController::UrlGenerationError: No route matches {:action=>”create”, :controller=>”dog”, :foo=>:bar} 我究竟做错了什么?

Rspec检查json响应

我正在尝试创建一个mock object ,我正在检查我的方法是否receives正确的param和预期的结果。 以下是我的spec 。 require ‘spec_helper’ describe User do let(:username) {“test@test.com”} let(:password) {“123”} let(:code) {“0”} context “when signing in” do it “should sign in” do user = double(“user”) expected_results = { “token”: “123” } allow(user).to receive(:login).with({email: username, password: password, code: code}) .and_return(expected_results) expect(user.login).to eq(expected_results) end end end 有没有办法将我的json与it隔离并将其保持在外面?

如何使用attr_accessible创建工厂?

如何处理工厂和attr_accessible ? 我的例子: # model class SomeModel attr_accessible :name, full_name, other_name end #spec require ‘spec_helper’ describe “test” do it do create(:some_model, name: “test name”, user: User.first) #factory end end #error ruby(17122,0x12a055000) malloc: *** error for object 0x8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 我认为错误是因为user_id不在attr_accessible属性中。

Rspec失败,“范围值不好”

我的方法: def swap (order) order = order == ‘asc’ ? ‘desc’ : ‘asc’ end 规格: let(:order) { ‘wrong_order’} it ‘swaps the order’ do expect(swap(order)).to eq(‘asc’) end 这个rspec失败了消息ArgumentError: bad value for range 但如果我通过’desc’或’asc’并改变期望它就可以正常工作。 另外我在irb上尝试了这个只是通过交换(”)它给了我’asc’不知道为什么rspec失败