Ruby ssh连接的Rspec
我正在尝试编写rspec来测试ssh连接。在我的spec文件中,即使我输入了不正确的服务器密码,它仍然显示Ruby ssh连接的Rspec,ruby,rspec,Ruby,Rspec,我正在尝试编写rspec来测试ssh连接。在我的spec文件中,即使我输入了不正确的服务器密码,它仍然显示0示例,0失败。有人能告诉我为什么我会看到这些,而我至少会看到一条失败的信息 下面是我的ssh\u host.rb和ssh\u host\u spec.rb文件的一段代码 require "java" require "highline/import" require 'open-uri' require 'socket' require 'rubygems' require 'net/ss
0示例,0失败
。有人能告诉我为什么我会看到这些,而我至少会看到一条失败的信息
下面是我的ssh\u host.rb
和ssh\u host\u spec.rb文件的一段代码
require "java"
require "highline/import"
require 'open-uri'
require 'socket'
require 'rubygems'
require 'net/ssh'
require 'stringio'
require 'net/scp'
require 'colorize'
module SshMod
class SshHost
attr_accessor :hostname, :username, :password
def initialize(host, user, password)
@hostname = host
@username = user
@password = password
@ssh = Net::SSH.start(@hostname, @username, :password => @password)
puts "\t Connection established for #{@hostname}...".blue
end
end
end
Rspec等级:
#!/usr/bin/env rspec
require 'spec_helper'
require 'ssh_host.rb'
describe SshMod::SshHost do
before :each do
@ssh = SshMod::SshHost.new "servername", "user", "wrong_password"
end
end
describe "#new" do
it "takes three parameters and returns sshhostobject" do
@ssh.should_be_an_instance_of SshHost
end
end
ssh_mock = double()
expect(SSH).to receive(:start).and_return(ssh_mock)
你的等级库文件有很多地方出错。对new
的测试应该在SshMod::SshHost
描述的上下文中,否则它无法访问ssh实例变量。另外,您的代码应该抛出一些错误,因为除外不是在内核中定义的,而是在Rspec对象的上下文中定义的。您很可能希望将其放在
之前的中
关于ruby类中的需求,我会去掉所有不需要的东西(例如,为什么在使用NetSSH时显式包含socket?)
然而,我相信,由于您的项目结构,您正在遇到没有测试运行的问题(但这只是一个猜测,因为您没有列出它)。默认情况下,Rspec查找在spec/***\u spec.rb
下列出的规范文件,您可以使用--pattern
标志覆盖这些文件。有关更多信息,请参见rspec--help
下面是一个代码的工作示例,其中包含了一些清理过的内容。我将代码的源代码放在lib
中,假设您正在制作类似宝石的东西
档案:
source "https://rubygems.org"
gem "colorize"
gem "rspec"
gem "net-ssh"
lib/ssh_host.rb
require 'net/ssh'
require 'colorize'
module SshMod
class SshHost
attr_accessor :hostname, :username, :password
def initialize(host, user, password)
@hostname = host
@username = user
@password = password
@ssh = Net::SSH.start(@hostname, @username, password: @password)
puts "\t Connection established for #{@hostname}...".blue
end
end
end
spec/spec_helper.rb
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'rspec'
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
spec/ssh\u host\u spec.rb
require 'spec_helper'
require 'ssh_host'
describe SshMod::SshHost do
let (:ssh) { SshMod::SshHost.new "servername", "user", "wrong_password" }
before :each do
allow(Net::SSH).to receive(:start).and_return(double("connection"))
end
describe "#new" do
it "takes three parameters and returns sshhostobject" do
expect(ssh).to be_a SshMod::SshHost
end
end
end
你的等级库文件有很多地方出错。对new
的测试应该在SshMod::SshHost
描述的上下文中,否则它无法访问ssh实例变量。另外,您的代码应该抛出一些错误,因为除外不是在内核中定义的,而是在Rspec对象的上下文中定义的。您很可能希望将其放在
之前的中
关于ruby类中的需求,我会去掉所有不需要的东西(例如,为什么在使用NetSSH时显式包含socket?)
然而,我相信,由于您的项目结构,您正在遇到没有测试运行的问题(但这只是一个猜测,因为您没有列出它)。默认情况下,Rspec查找在spec/***\u spec.rb
下列出的规范文件,您可以使用--pattern
标志覆盖这些文件。有关更多信息,请参见rspec--help
下面是一个代码的工作示例,其中包含了一些清理过的内容。我将代码的源代码放在lib
中,假设您正在制作类似宝石的东西
档案:
source "https://rubygems.org"
gem "colorize"
gem "rspec"
gem "net-ssh"
lib/ssh_host.rb
require 'net/ssh'
require 'colorize'
module SshMod
class SshHost
attr_accessor :hostname, :username, :password
def initialize(host, user, password)
@hostname = host
@username = user
@password = password
@ssh = Net::SSH.start(@hostname, @username, password: @password)
puts "\t Connection established for #{@hostname}...".blue
end
end
end
spec/spec_helper.rb
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'rspec'
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
spec/ssh\u host\u spec.rb
require 'spec_helper'
require 'ssh_host'
describe SshMod::SshHost do
let (:ssh) { SshMod::SshHost.new "servername", "user", "wrong_password" }
before :each do
allow(Net::SSH).to receive(:start).and_return(double("connection"))
end
describe "#new" do
it "takes three parameters and returns sshhostobject" do
expect(ssh).to be_a SshMod::SshHost
end
end
end